0

I'm trying to get a variable value from a number input without success.

HTML:

<input type="number" min="0" max="5" name="quantity" id="quantity" class="quantity" value="1" />

<div class="selectedvalue"></div>

SCRIPT:

$().ready(function() {
var quantity = $('input[type=number][name=quantity]').val();
  $(".selectedvalue").html(quantity);
});

JSFIDDLE

I only can get the attribute value (1)in the number input but not the actual selected value.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199

1 Answers1

0

Check the example inside W3schools. In your example, you use the value 1, and always show the value 1, because your set inside the tag.

You does not need jQuery for get this.

The type from your input is number, and add the id from your number for get the value by Id with document.getElementById("yourIdFromInput").value;

<!DOCTYPE html>
<html>
<body>

Number: <input type="number" id="myNumber" value="2">

<p>Click the button to display the number of the number field.</p>

<button onclick="myFunction()">Try it</button>

<p><strong>Note:</strong> input elements with type="number" are not supported in IE 9 and earlier versions.</p>

<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("myNumber").value;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The <p> with innerHTML will show inside html the value with this script.

Reference: all Explication about that here

Code W3schools.

Sayuri Mizuguchi
  • 5,250
  • 3
  • 26
  • 53