Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery - get first occurence of selection

How can i determine if first occurance (element 0) of contains text "No Errors"

if ($(xml).find('errors')[0].text() == 'No Errors') 
{
  do something
}

!!! edit !!!

 found it...

 if ($(xml).find('error').first().text() == 'No errors') 
like image 502
mustapha george Avatar asked Dec 13 '25 05:12

mustapha george


1 Answers

Using [0] causes JavaScript/jQuery to return the DOM node, instead of the jQuery object, you might try:

if ($(xml).find('.errors:first').text() == 'No Errors') 
{
  // do something
}

Or:

if ($(xml).find('.errors').eq(0).text() == 'No Errors') 
{
  // do something
}

Both of these if statements require that the text is, not simply contains, equal to 'No Errors'.

To test that the text contains the text 'No Errors':

if ($(xml).find('.errors').eq(0).text().toLowerCase().indexOf('no errors') > -1) 
{
  // do something
}

JS Fiddle demo.

References:

  • :first.
  • eq().
  • toLowerCase().
  • indexOf().
like image 107
David Thomas Avatar answered Dec 14 '25 20:12

David Thomas