-5

I tried to use jquery syntax to get number value from input field, but it does not work, the console did not show input value. Below is my code:

$(document).ready(function() {
  var num = $("#a").val();
  console.log(num);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="a" type="number">

9
  • i mean... What IS the input's value? Commented May 7, 2018 at 15:57
  • it does show it, it's just empty, try with <input id="a" type="number" value="123"> Commented May 7, 2018 at 15:58
  • if you want whatever you type to show up as you type, you need to tie the input to an onkeyup event. Commented May 7, 2018 at 16:00
  • Yup, I mean when you input a random number value in the input field, the console log does not show that number value. Commented May 7, 2018 at 16:02
  • 1
    Read some beginner tutorials on interacting with the DOM in JS. You'll learn how to wire up functionality to events caused by page interaction. And better to learn the native API instead of an old, large library like jQuery. Commented May 7, 2018 at 16:03

2 Answers 2

1

Add name="a" to your input element =>

See this for more information: jQuery .val() not working for input fields

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

Comments

0

Since you are using a document.Ready() event, the input value when the page loads is empty. If you want to update the console with the value when a user types a number, then you must bind the user event, such as with a .keyup() event:

    $("#a").keyup(function() {
        console.log($("#a").val());
     });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="a" type="number">

Comments

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.