-7

I am trying to get a value out of a <input type='num'> with JavaScript I am using the following code:

Choose a number between 1 and 5 <input type='num' name="input">

<button id="btn">Click me!</button>

<script>
    var input;

    document.getElementById('btn').onclick = function(){
    input = document.getElementById('num');
        alert(input); //To check what value input has
</script>

This should get a value but I just get a null what am I doing wrong?

5
  • 1
    getElementById('num') Where's the element with that ID? Commented Jun 12, 2015 at 12:57
  • 1
    document.getElementById() is supposed to return null if no element with the specified id is found. But in any case the code shown is incomplete and won't run. Commented Jun 12, 2015 at 12:57
  • What does this have to do with databases? Commented Jun 12, 2015 at 12:57
  • use this fiddle jsfiddle.net/nvtc7k68 Commented Jun 12, 2015 at 13:01
  • Sorry I am using this to get id's out of database's forgot to edit the name, Thanks for the help all Commented Jun 12, 2015 at 13:18

3 Answers 3

3

You have not defined your id. Also I guess your input type should be number.

<input type='number' name="input" id="num">
       ^^^^^^^^^^^^               ^^^^^^^^

And to alert its value you need to use

alert(input.value) //.value is used to get value of input
Sign up to request clarification or add additional context in comments.

Comments

2

There are more than one problems with your code

1) You have to close the bracket of your function it should be

 document.getElementById('btn').onclick = function(){
    input = document.getElementById('num');
        alert(input); //To check what value is outputted
}  

2)

input = document.getElementById('num');

The getElementById() method returns the element that has the ID attribute with the specified value.

so ID attribute is essential here and in your code there is no ID attribute defined so you have to defined it first

like

<input type='number' id="num" name="input">

3) document.getElementById('num'); does not return the value of input field it returns object so if you want value then use the following code

document.getElementById('num').value;

4) your input type="number"

for the desired output you can use following code

Choose a number between 1 and 5 <input type='number' name="input" id="myid">

<button id="btn">Click me!</button>

JS

    var myButton = document.getElementById("btn");
myButton.onclick = function()
{
         alert(document.getElementById("myid").value); //where does id come from?
}

The above method is pure JS if you need jquery method you can refer below

$( "#btn" ).click(function() {
  var input=$("#myid").val();
    alert(input)
});

Comments

1

getElementById() works on elements with id attribute. So, as you have not put id attribute in your input type, it is not able to find the element with id=num.
Just add id="num" in your input element and then you are good to go.

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.