Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass a variable by value to an anonymous javascript function?
    primarykey
    data
    text
    <p><strong>The Objective</strong></p> <p>I want to dynamically assign event handlers to some divs on pages throughout a site. </p> <p><strong>My Method</strong></p> <p>Im using jQuery to bind anonymous functions as handlers for selected div events.</p> <p><strong>The Problem</strong></p> <p>The code iterates an array of div names and associated urls. The div name is used to set the binding target i.e. attach this event handler to this div event.</p> <p>While the event handlers are successfully bound to each of the div events, the actions triggered by those event handlers only ever target the last item in the array.</p> <p>So the idea is that if the user mouses over a given div, it should run a slide-out animation for that div. But instead, mousing over div1 (rangeTabAll) triggers a slide-out animation for div4 (rangeTabThm). The same is true for divs 2, 3, etc. The order is unimportant. Change the array elements around and events will always target the last element in the array, div4.</p> <p><strong>My Code - (Uses jQuery)</strong></p> <pre><code>var curTab, curDiv; var inlineRangeNavUrls=[['rangeTabAll','range_all.html'],['rangeTabRem','range_remedial.html'], ['rangeTabGym','range_gym.html'],['rangeTabThm','range_thermal.html']]; for (var i=0;i&lt;inlineRangeNavUrls.length;i++) { curTab=(inlineRangeNavUrls[i][0]).toString(); curDiv='#' + curTab; if ($(curDiv).length) { $(curDiv).bind("mouseover", function(){showHideRangeSlidingTabs(curTab, true);} ); $(curDiv).bind("mouseout", function(){showHideRangeSlidingTabs(curTab, false);} ); } } </code></pre> <p><strong>My Theory</strong></p> <p>I'm either not seeing a blindingly obvious syntax error or its a pass by reference problem. Initially i had the following statement to set the value of curTab:</p> <pre><code>curTab=inlineRangeNavUrls[i][0]; </code></pre> <p>So when the problem occured i figured that as i changed (via for loop iteration) the reference to curTab, i was in fact <em>changing</em> the reference for all previous anonymous function event handlers to the new curTab value as well.... which is why event handlers always targeted the last div. </p> <p>So what i really needed to do was pass the curTab <em>value</em> to the anonymous function event handlers not the curTab <em>object</em> reference.</p> <p>So i thought:</p> <pre><code>curTab=(inlineRangeNavUrls[i][0]).toString(); </code></pre> <p>would fix the problem, but it doesn't. Same deal. So clearly im missing some key, and probably very basic, knowledge regarding the problem. Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload