Why doesn't jQuery accept this string?

When I run this function:

onUnCheck: function(el) {
var thenames = "icon-"+el.find("label:first").text().replace(/ /g,'').toLowerCase();        
alert(thenames);            
$("'."+thenames+"'").hide("fast");
}

I am generating a string. I see in alert () that this is indeed the correct line. Example:

icon-jira

But when I pass the string as a jQuery selector, it does not work.

I know that the logic of the function sounds because the insert as a result of my warning () makes it work.

Why doesn't jQuery accept my string?

+3
source share
2 answers
$('.'+thenames).hide("fast");

This will search for ".icon-jira". You searched for ".icon-jira" which adds quotation marks to the selector.

+5
source

It looks like you have an extra set of single quotes ( ''). Try the following:

onUnCheck: function(el) {
  var thenames = "icon-"+el.find("label:first").text().replace(/\s/g,'').toLowerCase();        
  alert(thenames);            
  $("."+thenames).hide("fast");
}

, /\s/g / /g - /\s/g .

+4

Source: https://habr.com/ru/post/1765858/


All Articles