javascript - Passing elements as variables to function -
i'm having issue element object , jquery function:
html
<label for='state'>state</label> <input id='state' name='state' type='text' value=''/> <span class='info'><img class='tick' /><img class='cross' /></span>
javascript / jquery
var state = $("#state"); function validatefield(myelement) { if (myelement.val().length > 3) { alert("word"); } else { alert("sup"); } } state.blur(validatefield(state)); state.keyup(validatefield(state));
nothing happens on page load, when state has more 3 chars entered.
any ideas?
awesome - learning new stuff ftw
no need arguments @ all, event handler bound element can use this
keyword inside function:
var state = $("#state"); function validatefield(event) { if (this.value.length > 3) { // <-- use `this.value` instead alert("word"); } else { alert("sup"); } } state.blur(validatefield); state.keyup(validatefield);
the way you're attempting call function , use return value event handler, why nothing happening:
// validatefield(state) executed , return value, "undefined" // passed first argument state.blur() state.blur(validatefield(state));
to fix other situations this
keyword not available, should use anonymous function:
state.blur(function () { validatefield(state) });
Comments
Post a Comment