I want to get the value of input in JavaScript after pressing submit but the problem is that I get the value I entered in HTML. After I press "Submit Query" the URL change to path/index.html?number=4 if I set the input to 4. But the value printed in console still the same '2'. I want to get the value of 4 not the default value located within HTML file!!
console.log(document.getElementById("number").value)
<input type="number" id="number" name="number" value="2">
<input type="submit" id="submit">
I want to get the value of input in JavaScript after pressing submit but the problem is that I get the value I entered in HTML. After I press "Submit Query" the URL change to path/index.html?number=4 if I set the input to 4. But the value printed in console still the same '2'. I want to get the value of 4 not the default value located within HTML file!!
console.log(document.getElementById("number").value)
<input type="number" id="number" name="number" value="2">
<input type="submit" id="submit">
Share
Improve this question
edited Jul 7, 2019 at 14:45
Bubbaloo
asked Jul 7, 2019 at 14:41
BubbalooBubbaloo
371 gold badge1 silver badge7 bronze badges
2 Answers
Reset to default 3You need to add an event listener for your button and then console.log
the result.
Try this:
var btn = document.getElementById('submit');
btn.addEventListener('click', func);
function func() {
console.log(document.getElementById("number").value)
}
<input type="number" id="number" name="number" value="2">
<input type="submit" id="submit">
You have to add and event listener to your submit button so that when you click on it, you get the value of your input each time
document.getElementById("submit").addEventListener("click", function(){
console.log(document.getElementById("number").value)
});
<input type="number" id="number" name="number" value="2">
<input type="submit" id="submit">