Maniraj Murugan
Maniraj Murugan

Reputation: 9084

How to get the changed input box value?

I am having jquery as follows,

$(document).on('click', '.save_button', function (e) { 
    var amount = $('.actual_pay').val();
});

Which means when a user clicks the button with class save_button, the script executes and sets the value of the var amount to the value of the field with class actual_pay.

Up to this no problems and it is working fine, but the input box with class name actual_pay is editable and any time the value can be changed, I have to detect the changed value of the input box, for which i have tried with

var amount = $('.actual_pay').val().change(); 

But it is showing error:

.change is not a function.

How to detect the change happening to the class actual_pay and to store the changed value to the amount variable?

Upvotes: 2

Views: 140

Answers (3)

void
void

Reputation: 36703

You can attach an input event listener.

var amount;
$('.actual_pay').on("input", function(){
  console.log("Something is changed in actual_pay");
  amount = $(this).val(); // save val
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="actual_pay">

Actions which invoke input events are

  • Entering some text into an element.
  • Cutting, deleting or pasting some content.
  • Dropping some content into an element. (only in Google Chrome and Safari)

Upvotes: 2

Rajesh kumar
Rajesh kumar

Reputation: 304

You can get value of that input box with this function:

$( "input.actual_pay" ).keyup(function() {

        var amount = $(this).val();
    });

If you are changing that input manually, you will use 'keyup', otherwise you should use

$( "input.actual_pay" ).change(function() {

        var amount = $(this).val();
    });

Upvotes: -1

Ayaz Ali Shah
Ayaz Ali Shah

Reputation: 3531

$('.actual_pay').val().change(); won't work because this is wrong approach. You can do by following method

$('.actual_pay').on('input',function(){
 alert('Something Modified');
});

Upvotes: 0

Related Questions