is there a way to pass $(this)
to another function in jQuery?
This is what I've got but it won't work:
$(function() {
$('[id$=price]').change(function() {
var price = $(this).val();
var quantity = $(this).closest('.fields').find('[id$=quantity]').val();
recalculate_subtotal($(this));
});
$('[id$=quantity]').change(function() {
var quantity = $(this).val();
var price = $(this).closest('.fields').find('[id$=price]').val();
recalculate_subtotal($(this));
});
function recalculate_subtotal(element) {
$(element).closest('.fields').find('#Subtotal').val(price * quantity);
}
});
(The last function is the one I want to get $(this) into.)
Thanks for any help and apologies. I am new to jQuery!
That will work fine, although one theing to note is that if you're passing the jQuery object ($(this)
) you dont need to wrap it again in $()
:
$('[id$=price]').change(function() {
var price = $(this).val();
var quantity = $(this).closest('.fields').find('[id$=quantity]').val();
recalculate_subtotal($(this));
})
function recalculate_subtotal($element) {
$element.closest('.fields').find('#Subtotal').val(price * quantity);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With