Try to search your question here, if you can't find : Ask Any Question Now ?

How to include a variable in a jQuery selector?

HomeCategory: stackoverflowHow to include a variable in a jQuery selector?
Avatarsourav asked 1 month ago

In my JavaScript, I have a variable called counter. And I want to append it to a select tag’s ID that I dynamically created using my script like-

counter = 5;

random_div.after().html('<select multiple class="js-example-basic-multiple" name="tal' + counter + '" id="tal' + counter + '" > </select>');

random_div.appendTo(main_div);

Now, I’m trying to resolve width of this newly created select tag, but it doesn’t work-

$("#tal" + counter).select2({width: 'resolve'});

I even tried it with another variable, but that doesn’t work either-

var testing = '#tal' + counter;
$(testing).select2({width: 'resolve'});

Now, if I hard-code this to say $("#tal5").select2({width: 'resolve'});, it works.

This is really elementary, but I can’t seem to figure out a solution.

1 Answers
Best Answer
Avatarnaveen answered 1 month ago
Your Answer

3 + 19 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com