How to access the value of this input field by its name attribute using Javascript
<input type='text' name='hey'>
document.querySelectorAll('input[name=hey]').value;
You were close, As querySelectorAll()
returns a list so you can use indexer to access the elements.
document.querySelectorAll('input[name=hey]')[0].value
better use querySelector()
document.querySelector('input[name=hey]').value
If your input name itself includes square brackets e.g. hey[]
, you would enclose the name in double quotes:
document.querySelector('input[name="hey[]"]').value
If you are flexible to use JQuery the here is the answer.
$("input[name=hey]").val();
Using Javascript, you can access it like this:-
document.getElementsByName('key')[0].value;
Look into this jsfiddle:-
https://jsfiddle.net/pLmvrdf3/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With