Reputation: 2522
Trying to toggle between divs and I found this:
Which works like a charm when the classes are static. I have a page where classes are variables:
var id = $(this).attr('id');
var cShow = "client_"+id;
var cEdit = "clientChange_"+id;
$('.'+cShow,'.'+cEdit).toggle();
how can i encapsulate the whole identifier in quotes.
the solution above is:
$('.class1,.class2').toggle();
mine becomes:
$('class1','class2').toggle();
Upvotes: 0
Views: 30
Reputation: 1092
Ben! If you are not bound to old browsers support, or using e.g. Babel compiler, you can use string interpolation for more neat solution.
$(`.${cShow},.${cEdit}`).toggle();
Or if you have an array of class names, just concatenate them with comma.
const classes = ['user_123', 'active', 'golden-style', 'hello-world'];
const classesStr = classes.map(c => '.' + c).join(',');
console.log(classesStr);
Upvotes: 0
Reputation: 53228
Your concatenation is slightly off, this should do the trick:
$('.'+cShow+',.'+cEdit).toggle();
Upvotes: 2