Reputation: 27507
I have one master text field, and I want it so that when the user types into this text field that the text fields with class variant_price
get updated with the same text.
I have it almost done:
$("#master_price").keypress(function (e) {
$(".variant_price").val($(this).val());
});
However, on the first keypress, it seems to fire off the $(".variant_price").val($(this).val());
part before e
happens, so when I type 123
the variant_price
text fields only get updated with 23
without the first keypress (1
).
How would I trigger e before that line? Is there anything like e.fire()
?
Upvotes: 0
Views: 57
Reputation: 27525
According to the use case you've described, you are looking for the change
event rather than keypress
(A field can be modified by pasting via mouse button, for example).
Also, you don't need the event object (e
) at all.
$("#master_price").change(function() {
$(".variant_price").val($("#master_price").val());
});
If you want an instant reaction to a change, you may bind the same handler to multiple events without storing the function as a temporary, as follows:
$("#master_price").on("change keyup", function() {
$(".variant_price").val($("#master_price").val());
})
Upvotes: 2
Reputation: 3019
Instead of keypress
you should use keyup
that should do the trick or change
as suggested:
$("#master_price").keyup(function(e) {
$(".variant_price").val($(this).val());
});
Because keypress
is triggered when the key is down and keyup
when its up again.
Upvotes: 3