Javascript question?

wsyeager

Centurion
Joined
Apr 10, 2003
Messages
140
Location
Weston, FL
Does anyone know what the syntax is to boldface and highlight an object in a different color?

I have the following javascript, but need to know the above to complete it:
<code>
function BoldUpdateDoc(){
var txt = document.getElementById("txtDetailedDescr");

}
</code>

Once I have a reference to the above object, I want to highlight it in a different color and boldface it...



On a different note, I'm trying to clear out a field on a form if a user clicks a linkbutton. I have the following javascript for that:
<code>
function ClearAttachmentDescr(){
var txt = document.getElementById("txtDetailedDescr");
txt = ''
}
</code>

However, when I try to set the onclick event in the html for the linkbutton to execute the above function, I get an error. I'm trying to prevent the control from executing the server-side postback when clicked, but can't seem to get it to work.

If anybody can help me with the above scripts, I'd really appreciate it.
 
I'm getting this error when I try and get to the web page:

'BoldUpdateDoc' is not a member of 'ASP.Request_aspx'


ASPX:
<code>
<asp:textbox id="txtDetailedDescr" style="Z-INDEX: 104; LEFT: 8px; POSITION: absolute; TOP: 64px"
tabIndex="10" runat="server" Height="36px" Width="488px" MaxLength="255" TextMode="MultiLine" OnTextChanged="BoldUpdateDoc(txtDetailedDescr.value);"></asp:textbox>
</code>


CODE-BEHIND:
<code>
lbtnClearAttachmentDescr.Attributes.Add("onClick", "ClearAttachmentDescr();")
txtDetailedDescr.Attributes.Add("OnTextChanged", "BoldUpdateDoc();")

If (Not IsClientScriptBlockRegistered("PageStartupJScript")) Then
RegisterClientScriptBlock("PageStartupJScript", "<script language=""javascript"" src=""ClientFunctions.js""></script>")
End If


function BoldUpdateDoc(text){
document.getElementById("txtDetailedDescr")).innerHTML = '<b>' + text + '</b>';
}
</code>

Any idea what I'm doing wrong?

wsyeager said:
Does anyone know what the syntax is to boldface and highlight an object in a different color?

I have the following javascript, but need to know the above to complete it:
<code>
function BoldUpdateDoc(){
var txt = document.getElementById("txtDetailedDescr");

}
</code>

Once I have a reference to the above object, I want to highlight it in a different color and boldface it...



On a different note, I'm trying to clear out a field on a form if a user clicks a linkbutton. I have the following javascript for that:
<code>
function ClearAttachmentDescr(){
var txt = document.getElementById("txtDetailedDescr");
txt = ''
}
</code>

However, when I try to set the onclick event in the html for the linkbutton to execute the above function, I get an error. I'm trying to prevent the control from executing the server-side postback when clicked, but can't seem to get it to work.

If anybody can help me with the above scripts, I'd really appreciate it.
 
To bold or any other style in JS, try changing its css class

document.getElementById("textbox").className = "custom-class";

to clear out the value use..

txt.value = "";

OnTextChanged="BoldUpdateDoc(txtDetailedDescr.value);"

boldupdatedoc is a client side function(JS), ontextchanged of a asp control wil point to a server side function(asp.net). instead do this.

txtDetailedDescr.attributes.add("onBlur","BoldUpdateDoc(txtDetailedDescr.value);")
 
I should have realized that about the difference in the server-side vs the client-side functions. I changed the code-behind to the following:
<code>

lbtnClearAttachmentDescr.Attributes.Add("onClick", "ClearAttachmentDescr();")
txtDetailedDescr.Attributes.Add("onBlur", "BoldUpdateDoc(txtDetailedDescr.value);")

If (Not IsClientScriptBlockRegistered("PageStartupJScript")) Then
RegisterClientScriptBlock("PageStartupJScript", "<script language=""javascript"" src=""ClientFunctions.js""></script>")
End If
</code>

I changed the html to the following:
<code>
<asp:textbox id="txtDetailedDescr" style="Z-INDEX: 104; LEFT: 8px; POSITION: absolute; TOP: 64px"
tabIndex="10" runat="server" Height="36px" Width="488px" MaxLength="255" TextMode="MultiLine"></asp:textbox>
</code>

I don't get an error on the page, but the functionality isn't working. The textbox is not getting clear out (when I click on the lbtnClearAttachmentDescr linkbutton) and the text is not being boldfaced for the other (when I change the text for the txtDetailedDescr textbox). As a matter of fact, the clearing out of the textbox still thinks it's server side, because when I view the source, I see the following for lbtnClearAttachmentDescr:
onClick="ClearAttachmentDescr();" href="javascript:__doPostBack('lbtnClearAttachmentDescr','')"

What am I still doing wrong???

kahlua001 said:
To bold or any other style in JS, try changing its css class

document.getElementById("textbox").className = "custom-class";

to clear out the value use..

txt.value = "";

OnTextChanged="BoldUpdateDoc(txtDetailedDescr.value);"

boldupdatedoc is a client side function(JS), ontextchanged of a asp control wil point to a server side function(asp.net). instead do this.

txtDetailedDescr.attributes.add("onBlur","BoldUpdateDoc(txtDetailedDescr.value);")
 
My guess is that your javascript needs debugging. Also..

onClick="ClearAttachmentDescr();" href="javascript:__doPostBack('lbtnClearAttachmentDescr','')"


you added a onClick attribute, but this asp control is posting back to the page. Do you need it to post back? If not, just use a regurlar html button and set the href="javascript:void(0)" or href = "ClearAttachmentDescr();"
 
I've placed the following javascript inside the page as follows:
<code>
Dim strScript As String = "<script language='javascript'>" & _
"document.getElementById('lbtnUpdateDoc').className = 'normal';" & _
"</script>"

Dim strScript2 As String = "<script language='javascript'>" & _
"if document.getElementById('lbtnUpdateDoc').className = 'normal'" & _
"{ document.getElementById('lbtnUpdateDoc').className = 'bold';}" & _
"</script>"

txtDetailedDescr.Attributes.Add("onBlur", strScript2)
Page.RegisterStartupScript("Normal", strScript)
Page.RegisterStartupScript("Bold", strScript2)
</code>

I want to boldface a link when the user starts typing in a textbox.

However, when I go into the webpage, and start typing, the link doesn't change to bold. I tried the following code, but as soon as I get into the webpage, the link is already boldfaced. It doesn't even wait until I start typing into the textbox.

<code>
Dim strScript2 As String = "<script language='javascript'>" & _
"document.getElementById('lbtnUpdateDoc').className = 'normal'" & _
"</script>"

txtDetailedDescr.Attributes.Add("onBlur", strScript2)
Page.RegisterStartupScript("Bold", strScript2)
</code>

Do you know off-hand, what I might be doing wrong?

In addition, how would you debug javascript from within VS.Net?


kahlua001 said:
My guess is that your javascript needs debugging. Also..

onClick="ClearAttachmentDescr();" href="javascript:__doPostBack('lbtnClearAttachmentDescr','')"


you added a onClick attribute, but this asp control is posting back to the page. Do you need it to post back? If not, just use a regurlar html button and set the href="javascript:void(0)" or href = "ClearAttachmentDescr();"
 
Page.RegisterStartupScript("Bold", strScript2)

You are runing the if..then check for bold face as the page is being rendered. Instead, put it in a function that gets called in your onBlur.

like .attributes.add("onBlur","change_class('lblLabel');")

then

<script language="javascript">

function change_class(labelid) {
if document.getElementById(labelid).className = 'normal'
{ document.getElementById(labelid).className = 'bold';}
{
</script>
 
What you said makes perfect sense...

I tried to implement the code as follows:
<code>
Dim strScript As String = "<script language='javascript'>" & _
"function change_class('lbtnUpdateDoc') " & _
"{if document.getElementById('lbtnUpdateDoc').className = 'normal'" & _
"{ document.getElementById('lbtnUpdateDoc').className = 'bold';}" & _
"}" & _
"</script>"

txtDetailedDescr.Attributes.Add("onBlur", "change_class('lbtnUpdateDoc');")

Page.RegisterStartupScript("Bold", strScript)
</code>

However, the script is still not working. It's not changing to boldface when I type in the textbox named (txtDetailedDescr).

What am i doing wrong?
 
A few things I noticed.

If this js function is for one purpose, no need to pass in the link's id.

Do the style classes "normal" and "bold" exist?

Also, in js, when you compare two values, you use ==, when you set a value, you use =

so...

if (myValue == "yes") {

}
 
The styles do exist as outlined in the below code:
<code>
.bold
{
font-weight:bold;
}
.normal
{
font-weight:normal;
}
</code>

The stylesheet and the page I'm trying to get the script to work with are both on the root level of the web app as follows:
<code>
<LINK href="Styles.css" type="text/css" rel="stylesheet">
</code>

I have the following js when registering my scripts:
<code>
Dim strScript As String = "<script language='javascript'>" & _
"function change_class() " & _
"{if document.getElementById('lbtnUpdateDoc').className == 'normal'" & _
"{ document.getElementById('lbtnUpdateDoc').className = 'bold';}" & _
"}" & _
"</script>"

txtDetailedDescr.Attributes.Add("onBlur", "change_class();")

Page.RegisterStartupScript("Bold", strScript)
</code>

All of this seems to check out with me.... I've registered javascripts in other apps with a separate file (which for some reason isn't working in this app - that's why I'm creating the script somewhat inline).

If you could tell me anything of waht's wrong with the way I'm doing it, I'd much appreciate it. I just think I'm trying to jump thru hoops just for some simple javascripts to be done. I think MS should have come up with a simpler way to incorporate JS into code without making a mountain out of a mole hill...

Thanks in advance....
 
Use this..
Code:
Dim strScript As String = "<script language='javascript'>" & _
"function change_class() { " & _
"if (document.getElementById('lbtnUpdateDoc').className == 'normal') " & _
"{ document.getElementById('lbtnUpdateDoc').className = 'bold'; }" & _
"}" & _
"</script>"

Also, make sure your lbtnUpdateDoc default cssclass="normal"

This works for me.
 
JavaScript question...

I copied and pasted your exact code into mine, and it still doesn't work...

Something is obviously being hosed up on my end. MS should have a much easier way to implement JS into your code and it should have a way to debug your JS as well. I just can't figure out where the problem is happening.

I even tried changing the code to the following:
<code>
Private Sub RegisterStartUpScripts()

txtDetailedDescr.Attributes.Add("onBlur", "change_class();")

If (Not IsClientScriptBlockRegistered("PageStartupJScript")) Then
RegisterClientScriptBlock("PageStartupJScript", "<script language=""javascript"" src=""~/ClientFunctions.js""></script>")
End If

End Sub
</code>

The JS code in my ClientFunctions.js file is as follows:
<code>
function change_class()
{
if document.getElementById("lbtnUpdateDoc").className == "normal"
{ document.getElementById("lbtnUpdateDoc").className = "bold";
}
}
</code>

The above still doesn't work as well.

The thing that puzzles me, is that I've done this type of stuff before without too much of a problem. For some reason, it just is hosing up now....

Thanks for your patience and help in this matter. If you have any other solutions, feel free to share them with me, especially to see if the javascript function is even being rendered and if it is properly executing....
 
I duplicated what you have and it works on my end. When you click out of the textbox, do you see any browser js errors? If you view your html, do you see the function? You didnt copy my js code exactly, look at it again, the if..is in ( ). If that doesnt work, we can take it a step at a time. Begin by just making sure we can call the js function, perhaps put a alert("test") inside that function, then we can know right off the bat if we are even getting in there.
 
Back
Top