Click here to Skip to main content
15,559,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here's the code:
C#
Popups = [{c:'#Payment-Selection',w:'#Cards-Popup'},{c:'#Drop-SY',w:'#Start-Years'}];
var ActivatePopups = function() {
    for (var i=0; i<Popups.length; i++) {
        $(Popups[i]['c']).click(function() { PopupDrop(Popups[i]['w']); });
    }
}
ActivatePopups();



Actual Popups array is much longer, thats why I want to use such method. Anyway, this does not work because there is problem when I define function bolded function.

The click is triggered, but it seems, that the firing function is not complete, because I'm getting such error¬:
'Uncaught TypeError: Cannot read property 'w' of undefined'
Posted
Updated 17-Feb-14 11:50am
v4

It hard to say what's wrong without having your code for PopupDrop; but let's write it in finer detail. Maybe you meant to do something different. Let's see:
JavaScript
var ActivatePopups = function() {
    for (var i=0; i<popups.length;>
        cElement = $(Popups[i].c); // jQuery wrapper for the element found by the id selector
        // also, pay attention for better syntax for using indices 'c' and 'w'
        wElement = $(Popups[i].w); // jQuery wrapper for the element found by the id selector
        // and you simply used Popups[i]['w']; that would be some string, not element
        // did you actually mean to get the element by id selector? it would be more logical
        // adding the handlers:
        cElement.click(function() {
            PopupDrop(wElement); // maybe this is what is required by PopupDrop?
        });
    }
Please try. It this won't help you, you need to provide more detail on your code.

Generally, it's a good idea to use separate variables and assign the DOM element wrappers to them; that way, you can always reuse the element wrapper without using selectors again and again (no, you did not to this mistake in your code, this is just the potential problem).

—SA
 
Share this answer
 
v3
Comments
Tautvydas Deržinskas 17-Feb-14 17:54pm    
So lets say full code is:
<pre lang="Javascript">function PopupDrop(n) { console.log(n); }

var Popups = [
{c:'#Payment-Selection',w:'Cards-Popup'},
{c:'#Drop-SY',w:'Start-Years'}
];

function ActivatePopups() {

for (var i=0; i<Popups.length; i++) {

$(Popups[i]['c']).click(function() { PopupDrop(Popups[i]['w']); });

}

}
ActivatePopups();</pre>
It does not work for me, using newest version of jquery. Thats not the case with elements, I just need to pass second object option to function as string...
Tautvydas Deržinskas 17-Feb-14 18:08pm    
P.s. In this case if I will click on <div id="Payment-Selection"></div> I will get error: 'Uncaught TypeError: Cannot read property 'w' of undefined ', line: $(Popups[i]['c']).click(function() { PopupDrop(Popups[i]['w']); }); http://jsfiddle.net/s5auB/
Sergey Alexandrovich Kryukov 17-Feb-14 18:34pm    
Why would you need to log selector string?
—SA
Sergey Alexandrovich Kryukov 17-Feb-14 18:35pm    
Also, can you run this under the debugger? Use my longer variant of code. Locate exception throwing precisely. The problem looks quite simple...
—SA
I fixed it using call back function:

JavaScript
function PopupCallBack(x) {
   return function (e) {
      PopupDrop(x);
   };
}
function ActivatePopups() {
    for (var i=0; i<popups.length;>    console.log('On click here: '+Popups[i]['c']+' do this:'+Popups[i]['w']); }
}

More information about problem: Line by Line explanation of "Javascript: Good Parts" example?[^]

Thanks for the answer to Tomasz Kowalczyk
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900