Assign Value to an Input Box not getting assigned

Qamar, Mo 106 Reputation points
2022-04-04T15:14:26.08+00:00

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

Developer technologies ASP.NET Other
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 30,126 Reputation points
    2022-04-04T15:31:24.573+00:00

    Server controls render unique IDs so you need the ClientID.

    document.getElementById("<%=MyInputBox.ClientID%>").value = "My value 1";
    

1 additional answer

Sort by: Most helpful
  1. Albert Kallal 5,586 Reputation points
    2022-04-04T16:35:34.203+00:00

    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:

    189799-image.png

    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:

    189842-mk1.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.