rob.m
rob.m

Reputation: 10571

How to retrieve the value of an input on keyup?

What I am trying to achieve is to output the actual value/text so that I can save it into a variable, but the console gives me a number indicator and it keeps adding to it each time I keyup.

<input id="usp-custom-3" type="text">
var country1 = $("#usp-custom-3").html();
$("#usp-custom-3").on("keyup", function() { 
    console.log(country1);
});

Upvotes: 1

Views: 51

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337560

There's two issues with your code. Firstly you need to get the val() of the input, not it's html(). Secondly, you need to retrieve the value every time the event happens, so place it within the event handler, like this:

$("#usp-custom-3").on("keyup", function() {
  var country1 = $("#usp-custom-3").val();
  console.log(country1);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="usp-custom-3" type="text">

Upvotes: 3

Related Questions