Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the value of a input number in jQuery

I'm working on a form, and I want to get in real time the current value of an input field of type number, as this one :

$(":input").bind('keyup mouseup', function () {
  var coins = $("#coins").val();
  $("#reward").text(coins);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<input id="coins" type="number" name="coins" value="1" max="999" min="1">

<p id="potential">Potential reward : <b id="reward"></b></p>

But nothing happen when I run it. It says that the value of coins is null. I don't know what I am doing wrong... Anyone could help ?

like image 688
Louis 'LYRO' Dupont Avatar asked Apr 02 '17 14:04

Louis 'LYRO' Dupont


People also ask

How to get the value from an input in jQuery?

jQuery val() method is used to get the value of an element. This function is used to set or return the value. Return value gives the value attribute of the first element.

How to get value from text in jQuery?

To get the value of a script element, use the . html() method. As of jQuery 1.4, the . text() method returns the value of text and CDATA nodes as well as element nodes.

How to get set value in jQuery?

To get a form value with jQuery, you need to use the val() function. To set a form value with jQuery, you need to use the val() function, but to pass it a new value.


1 Answers

You could use the following:

$(document).on('input', '#coin', function(){
    var coins = $("#coins").val();
    $("#reward").text(coins);
})
like image 145
zeus Avatar answered Oct 12 '22 23:10

zeus