Tachi
Tachi

Reputation: 2222

How to prevent variable being treated like a string?

I must target specific label element using its for parameter, and I must do it in a forEach cycle, like this:

 var iw_signup_tooltip = function(elements) {
     elements.forEach(function(entry) {
         $('<i class="icon"></i>').appendTo($('label[for=entry]'));
       }
     }

And then I'm calling it like this:

var elements = ['label1', 'label2'];
iw_signup_tooltip(elements);

But for some reason function fails and doesn't attach anything. But if I manually replace entry with 'label1' for example it works fine. Or even if I put label1 without '' it will still work, so I guess there could be a bug, or am I doing something wrong?

Upvotes: 0

Views: 38

Answers (1)

tymeJV
tymeJV

Reputation: 104785

String concatentation! You need to write the entry variable in - it won't be picked up automatically.

 $('<i class="icon"></i>').appendTo($('label[for=' + entry + ']'));

Upvotes: 3

Related Questions