Sunday 15 January 2012

How do i pass an id as a parameter for a function using the click eventListener in jquery? -



How do i pass an id as a parameter for a function using the click eventListener in jquery? -

<script> $( document ).ready(function() { //for illustration item button clicked , panelcontent panel shown $('#item').click({param1: '#panelcontent'}, toggleobjects); // tried , doesn't work $('#item').click(toggleobjects('#panelcontent')); }); var currentobject; function toggleobjects(theobject) { //theobject id of panel if(currentobject===null) { currentobject=theobject; $(currentobject).show(); } if(currentobject===theobject) { $(theobject).toggle(); } if(currentobject!==theobject) { $(currentobject).hide(); currentobject=theobject; $(currentobject).show(); } console.log( 'executed!' ); } </script>

if set onclick="toggleobjects('#panelcontent')" on button in html works fine, utilize jquery event listener.

how should utilize .click event passing function , parameter? or there solution besides html onclick version ?

please excuse poor english! give thanks in advance attending matter.

what appears want either in-line function--

$('#item').click(function() { toggleobjects('#panelcontent'); } );

or define new function calls toggleobjects function specific parameter --

var toggleobjectswithpanelcontent = function() { toggleobjects('#panelcontent'); }; $('#item').click(toggleobjectswithpanelcontent);

.

you got close $('#item').click(toggleobjects('#panelcontent'));,

but toggleobjects('#panelcontent') bit function phone call there, not reference function, you're passing homecoming value of toggleobjects click() fn. (the homecoming value in case beingness undefined).

jquery parameters

No comments:

Post a Comment