JavaScript

Tony Brackins
Tony Brackins
26,244 Points

Change a value of an input

Hello all, I need help figuring out how to change a value of an input using JavaScript.

I thought just using the .value= "" would do the trick, however, it made no change.

I used: document.querySelectorAll("input[name=inputName]").value = "test"; but the value didn't change.

Any suggestions?

1 Answer

leong shing chew
leong shing chew
5,618 Points

If you have the id of the element, you could :

var element = document.getElementById('element_id');
element.value = 'random_value';

or if you want to use querySelectorAll, assuming there is only one element that matches the query:

var elements = document.querySelectorAll("input[name=inputName]");
elements[0].value= 'test';

It doesn't work in your example because querySelectorAll return a nodeList rather than a single object that you are using it for.