javascript - How can I read ".on()" method arguments in jQuery? -
i have no idea how bite problem. below 2 examples working great want avoid dry problem.
parentelement.on('focusout', '.foo-class', function () { // console.log('hello foo') });
and:
parentelement.on('focusout', '.bar-class', function () { // console.log('hello bar') });
i make more universal. have deal 2 classes while parent stays same.
assuming first step:
parentelement.on('focusout', classvalue, function () { // how display class can call different stuff depending on class value? // console.log('hello ' + classvalue) });
may along these line, refined guardio's solution.
fiddle: https://jsfiddle.net/pvorhknv/2/
what doing here element been called handler , accessing attributes. can use element "this" such use.
$(document).on('focusout', 'input', callme); function callme(){ console.log('hello ' + $(this).attr('class').split('-')[0]) }
update:
one other thing can use mark data attribute elements. fiddle: https://jsfiddle.net/pvorhknv/3/
<input type='text' class='foo-class' data-classname="foo"> <input type='text' class='bar-class' data-classname="bar">
and hence can access them,
function callme(){ console.log('hello ' + $(this).data('classname')); }
Comments
Post a Comment