Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
12
rated 0 times [  18] [ 6]  / answers: 1 / hits: 16023  / 13 Years ago, mon, august 1, 2011, 12:00:00

I'm trying to set the text on a button in a JScript function. I am using a JScript function like this...



    function myfunction(sender, eventArgs)
{
wanted_text = window.source_text_field.get_displayText();

if (document.getElementById(my_span_element) != null)
{
document.getElementById(my_span_element).innerText = wanted_text;
}

if (document.getElementById(my_button) != null)
{
document.getElementById(my_button).text = wanted_text;
}

}


where the controls concerned are define like this...



    <span id=my_span_element class=some_class></span>

<asp:Button id=my_button
runat=server
class=some_other_class
text=
Width=48
Height=25
onclick=do_foo/>


The text in the span element is set correctly but the button is unaffected. I've also tried innerText, Value, Text, and also attempted to use the answer to this question but to no avail.



Can anyone see what I have overlooked?


More From » asp.net

 Answers
76

There are 2 problems here:



First: you use wrong attribute - should be value, instead of text



Second: document.getElementById(my_button) will not work because ASP.NET assigns it's own ids to controls. If you take a look at generated markup you will notice that id of the element is completely different.



If your javascript is inside of the same .aspx page as <asp:button /> control, you can do like this:



document.getElementById(<%= my_button.ClientID %>).value = some text;

[#90884] Saturday, July 30, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aidan

Total Points: 72
Total Questions: 95
Total Answers: 121

Location: Uzbekistan
Member since Sat, Feb 27, 2021
3 Years ago
aidan questions
Mon, Oct 11, 21, 00:00, 3 Years ago
Wed, Sep 29, 21, 00:00, 3 Years ago
Sun, Sep 5, 21, 00:00, 3 Years ago
Thu, Jan 16, 20, 00:00, 4 Years ago
;