If an element contains a string

I had a question today if I could change the background color if an element contained a string. I knew I could with js, but I need to do a little research.

I found methods of completing this.

indexOf

The indexOf() method returns the position of the first occurrence of a specified value in a string.
This method returns -1 if the value to search for never occurs.
indexOf is case sensitive.

:contains

The :contains() selector selects elements containing the specified string.
The string can be contained directly in the element as text, or in a child element.
This is mostly used together with another selector to select the elements containing the text in a group (like in the example above).
:contains is case sensitive.

if ($('#someId').text().indexOf("Gene") != -1)
{
  $('body').css( "background-color", "blue" );   
}
$( "#someId:contains('Gene')" ).css( "background-color", "blue" ); 
This entry was posted in Uncategorized. Bookmark the permalink.

Comments are closed.