This post will discuss how to set the value of an input text box in JavaScript and jQuery.

1. Using JavaScript

With JavaScript, the idea is to access the native value property and set its value:

JS


HTML



Edit in JSFiddle

 
Alternatively, you can use the setAttribute() method to set an attribute’s value on the input text box.

JS


HTML



Edit in JSFiddle

2. Using jQuery

With jQuery, you can use the .val() method to set the value of an input text box, as shown below. Note, this method does not trigger the change event, but you can manually call the change event after setting the value using the .change() or .trigger("change") method.

JS


HTML



Edit in JSFiddle

That’s all about setting the value of an input text box in JavaScript and jQuery.