I want to pass a reference of the DOM object to a jQuery function. However the function does not get a reference to the DOM object. It does get a string containing the DOM object. So i get an error on the following example
<input type="text" size="30" onchange="change_total_price(this)" id="priceField">
.
function change_total_price(input) {
input.closest('div').find('#amountField').val());
}
You need to wrap it, like this:
this, beinginputin the function is a DOM element, but.closest()is a jQuery object method so you need to wrap it in$()to get a jQuery object if you want to use it.Alternatively do this outside of your markup, like this:
This way binds the
changeevent handler to theid="preiceField"element, you can remove the in-lineonchange="change_total_price(this)"…it’s both cleaner and easier to maintain (can be outside your page in an external.jsloaded and cached once, for example).As an aside, IDs should be unique, so it doesn’t need to be relative at all, both of the above examples can just be: