Server controls render unique IDs so you need the ClientID.
document.getElementById("<%=MyInputBox.ClientID%>").value = "My value 1";
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
I have an Input box and I am trying to assign a value to it in JavaScript. I have tried both the "Id" and the "Name"property but still not getting the value assigned to it.
What am I doing wrong
189802-code.txt
Server controls render unique IDs so you need the ClientID.
document.getElementById("<%=MyInputBox.ClientID%>").value = "My value 1";
Well, do you have jQuery installed? (most do).
So, I tend to do it this way:
Since I tend to only have a say a few controls on a page? Then in markup, I tend to add the ClientIdMode="static"
This just makes the js code oh so much cleaner.
but, using jQuery, then say this:
So, in above, the first 2 controls are asp.net ones (with clientidmode="static".
The next 2 controls are HTML controls, (input box, and label).
So the above code is quite simple. Note that if for ONLY controls with runat="server"?
Well, if you don't have clientIDMode="static", then of course you need to change the js code to this: