I have the following jQuery snippet which sets the 'selectedIndex' property of six select elements to '0'.
for (m = 1; m <= 6; m++) {
$("#po"+eval("leafWId"+m)).prop("selectedIndex", 0);
}
In the snippet scope there are among others the following variables defined: leafWId1, leafWId2, leafWId3, leafWId4 with each one of these set to a different number, so the part
eval("leafWId"+m)
in each iteration, is equivalent to
eval("leafWId1"), eval("leafWId2"), eval("leafWId3")........
and thus, evaluates to each of the numbers referred above, and the snippet
$("#po"+eval("leafWId"+m))
returns a jQuery object consisting of select elements with id value of the type: "po345" for example.
Now, when the 'm' variable is set through the iteration to value '5', I get an Uncaught Reference Error, reporting that "leafWId5 is not defined" (as expected, since only four variables are defined, ie, leafWId1, leafWId2, leafWId3, leafWId4 as mentioned above).
I want to add a conditional statement that will check for an undefined variable whose name will be the result of the evaluation
eval("leafWId"+m)
and if is defined, then use the prop() method, if not, skip this, so I dont' get the Reference Error.
Or as an alternative, maybe check for the length of the jQuery object below
$("#po"+eval("leafWId"+m))
and proceed with the prop() method only when it's length is greater than zero.
Actually, the problem I face is that when m variable is set to '5', the part
eval("leafWId"+m)
evaluates to 'leafWId5', a variable that is not defined, and the snippet does not let me check for 'undefined' value in advance, because the eval() function gives an error, so any conditional statement I have used that checks for existing variable and uses the eval() function does not work.