javascript - Set "this" to an selector inside if statement when no event listeners triggered -


i trying set "this" selector inside if statement via jquery when there no event listeners triggered. @ moment have write alot of code achieve behaviours seen below

function show(){     if($('input#q1male').is(":checked")){             //do something...     } else {             //do else...     }     if($('input#q1female').is(":checked")){             //repeated something...     } else {             //repeated else...     } }show(); 

if "this" can applied inside if statement if simplify , clean javascript. tried looking .call() , .apply() without luck.

you use jquery.bind before calling function set context(='this'), or said, use apply/call when calling function.

apply/call accept first argument context of function,

e.g :

show.apply(wantedcontext, [arg1, arg2,...]); 

or :

show.call(wantedcontext, arg1, arg2,...); 

in case wantedcontext $('input#q1male'), function :

function show(){     if(this.is(":checked")){             //do something...     } else {             //repeated else...     } } 

and call using :

show.call($('input#q1male'));  

and

show.call($('input#q1female'));  

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -