25

I want to set an HTML field's value using JavaScript when onclick event is raised by clicking a button/hyperlink.. I Googled and found many articles using the name attribute, but I want to set the value using the field's id only.

1
  • What have you tried .. The concept is the same as using name, a simple trial and error answer... Commented May 20, 2013 at 3:09

1 Answer 1

51
document.getElementById('Id').value='new value';

https://developer.mozilla.org/en-US/docs/Web/API/document.getElementById

Sign up to request clarification or add additional context in comments.

8 Comments

In my opinion just the fact that w3schools charges money to issue certificates that no employers are likely to recognise is reason enough to avoid them. Also the actual name "w3schools" seems like a cynical attempt to trick people into thinking they have some association with the w3c when they really don't, so that's another reason to steer clear.
@Jayantha: the others are right, linking W3Schools as a reference is really bad. Please change your link to MDN (Mozilla's Developer Network): developer.mozilla.org/en-US/docs/Web/API/…. Thanks!
Ok got it, I will change. Thanks for the info.
Do i need to refresh it after set the value ... it didnt reflect on the UI
@sparsh610, no you don't need to refresh it should reflect immediately. If you refresh the browser it will be gone again.
|

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.