1

I want to retrieve the textbox value in javascript. Simple, but it doesn't work for me, as it returns null/undefined as output. The code:

<asp:TextBox 
    ID = "lbl1" 
    ClientIDMode = "Static" 
    runat = "server"    
    Text = "http://test/test1/dilse.mp4" 
    Visible = "false">
</asp:TextBox>
<asp:Button  
    ID = "btnValidate" 
    runat = "server" 
    Text = "Get Values"   
    OnClientClick = "sourceval(); 
/> 

Javascript:

var source = document.getElementById('lbl1').innerHTML;
function sourceval() 
{
    alert(source); 
}
1
  • Where are you calling your javascript? It may be that the textbox doesn't exist fully at the time of your calls to access it. Commented Feb 19, 2014 at 20:54

3 Answers 3

3

you're not able to get the value of the textbox because you have Visible="false", because of that the textbox doesn't get rendered when the page loads, you could use a HiddenField instead of textbox

<asp:HiddenField ID ="lbl1" ClientIDMode="Static" runat="server" Value="http://test/test1/dilse.mp4" />
Sign up to request clarification or add additional context in comments.

Comments

2

Use the .value property for textboxes. The code for setting OnClientClick is for VB. Use '+' instead of '&' if using C#.

<asp:Button ID="btnValidate" runat="server" Text="Get Values"
         OnClientClick='<%= "sourceval(" & lbl1.ClientID & " );"%>' /> 

Javascript code:

    function sourceval(controlId) {
          var source = document.getElementById('lbl1').value;
          alert(source); 
    }

6 Comments

HI, thanks for the reply. i used .value but it says undefined in my output.
Try passing in source as a parameter?
I just saw the problem -- you're using webforms, which autogenerates random IDs based on html element structure. I will update my code sample
Updated. Let me know if it works--- I can't remember if the page will know that ID at runtime, but I assume it would.
hi, its works now as i have changed the textbox to hiddenfield as Juan C Suggested,thanks for your time
|
0

Just use:

var source = document.getElementById('lbl1').value;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.