Sara
Sara

Reputation: 14628

Monitoring a value change on an input element

US Price: <input type="text" name="us_price" id="us_price" value="1000" onchange="$('#uk_price').val($(this).val()*2);" />
UK Price :<input type="text" name="uk_price" id="uk_price" value="2000" />

Here if a user enters a value in US price it will double and set in UK Price. What I want is to write a separate function to monitor the value change in UK price input field. Please help.

for example: if value changed show alert("Done!").

Upvotes: 0

Views: 1602

Answers (1)

gdoron
gdoron

Reputation: 150253

$('#uk_price').change(function(){
    alert('changed! the value is now: ' + this.value);
    // You can assign the value to the us_price input
    $('#us_price').val(this.value));
});

Note that the code you have:

$('#uk_price').val($(this).val()*2);

won't trigger the change event, because it wasn't focused.

This should do the trick:

$('#uk_price').focus().val(this.value * 2);
$(this).focus();

Upvotes: 3

Related Questions