0

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.

vasilis
  • 143
  • 1
  • 1
  • 13
  • 2
    do not use eval() for string concatenation. So you have a variable `leafWId0` and you want to use that? – epascarello May 29 '18 at 15:09
  • Please never, ever, *ever* use `eval()`. An object with keys that you access via bracket notation is a much better alternative. Better still use an array you access by index. – Rory McCrossan May 29 '18 at 15:10
  • 1
    @epascarello - Not being used for concatenation here, being used to look up runtime-defined variable names. – T.J. Crowder May 29 '18 at 15:12
  • @RoryMcCrossan Well, there *are* some legitimate use cases for `eval`, but the general rule is that if you think you need to use `eval`, you probably don't. – Derek 朕會功夫 May 29 '18 at 15:18
  • try { eval(leafWId75) } catch (e) { console.log('no reference error'); continue; } – ahrobins May 29 '18 at 15:41
  • first a fall dont ever go with eval, but if you wanna proceed and proceed further only when the html element exists then you can wrap the statement inside a conditional IF statement as, `if ( $(“#po”+eval(“leafWid”+m)) ) { write your statement }` – Abhishek Kumar May 29 '18 at 16:07

0 Answers0