Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery text() compare to string

How to compare strings to jquery text()? I've tried this one but it will not work.

var str = 'hello';
var string = $(this).find("text").trim();

if(str == string)
{          
   string.css("visibility", "visible");
}

What's wrong with my code? Any idea please? thank you

like image 927
Lyner Kharl Avatar asked Jun 05 '26 11:06

Lyner Kharl


1 Answers

If you want to use the text method you need to actually use it. Using find("text") would try fo find a <text> element inside the element, and there are no such elements.

Also, use the jQuery trim method as the string.trim method isn't available in all browsers. Apply the style to the element, not the string:

var str = 'hello';
var string = $.trim($(this).text());

if(str == string)
{          
   $(this).css("visibility", "visible");
}
like image 175
Guffa Avatar answered Jun 07 '26 02:06

Guffa