Click here to Skip to main content
15,908,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this code in by .aspx page:

aspx
<div>
<asp:textbox id="txt1" runat="server" value=""/>
</div>



I want to use javascript to select the value of the textbox in the DIV tag.

Please tell me how I can do this.

Thanks very much everybody!
Posted
Updated 13-May-11 5:47am
v2

Since the id of a tag with runat="server" has nothing to do with the id attribute of same tag on the client (after it has been rendered to HTML) you'll have to do this in the javascript of your .aspx page:

JavaScript
function showMyTextBoxValue()
{
    var textBox = document.getElementById("<%= txt1.ClientID %>");
    alert(textBox.value);
}


Alternately you can have the javascript code outside your .aspx page:

JavaScript
function showMyTextBoxValue(elementName)
{
    var textBox = document.getElementById(elementName);
    alert(textBox.value);
}


but this means when you call this javascript function (linked into your .aspx page via script tag with src attribute) on your .aspx page you'll have to do something like this:

XML
<input id="test" name="test" value="Show textbox value!" onclick="showMyTextBoxValue('<%= txt1.ClientID %>')"/>


Best Regards,

-MRB
 
Share this answer
 
Comments
parmar_punit 17-May-11 2:15am    
good answer my 5
 
Share this answer
 
Hi,

you can use this
document.getElementById("<%= txt1.ClientID %>").value;

or if you are using Jquery you can use
$("#<%= txt1.ClientID %>").val();


Hope this will help.
 
Share this answer
 
var tid=document.getelementbyid("tb1").value
 
Share this answer
 
document.getElementById("<%= txtbox.ClientID %>").value;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900