[jQuery] Re: using :contains() selector with $(this)

2007-12-13 Thread Wizzud

As with anything else there are a number of ways of going about this,
and which one you use depends a lot on what you want to do with the
result.
One thing to point out with your $(this:contains(...)) example is that
'this' is usually an object variable and ':contains(...)' is a string
and you can't just tack them together and expect them to work.

eg.
// assuming (for example)...
var toMatch = 'Text 2';

// then
var doMatch = $( 'ul.list a:contains(' + toMatch + ')' );
if( doMatch.length ){
  // got one, and its now held in doMatch
}else{
  // not found
}

// or...
$( 'ul.list a:contains(' + toMatch + ')' )
.each(function(){
   // do something with the matching anchor ...
  });

// or...
$( 'ul.list a' ).filter( ':contains(' + toMatch + ')' )
.each(function(){
   // do something with the matching anchor ...
  });

//  or...
$( 'ul.list a' ).each(function(i){
if( $(this).text() == toMatch ){
  // do something with the matching anchor ...
}
  });

//  or...
var selector = ':contains(' + toMatch + ')';
$( 'ul.list a' ).each(function(i){
if( $(this).is(selector) ){
  // do something with the matching anchor ...
}
  });

//  or...
$( 'ul.list a' ).each(function(i){
if( $(this).text().indexOf(toMatch)  -1){
  // do something with the matching anchor ...
}
  });

and there are a number of other variations possible too!
Note that all the above assume that it is the matching anchor you want
to end up with. If it's not the anchor you want but the list item
containing that anchor, then either use parent() on the resulting
element, or modify the query slightly.

As I said at the start, which method (or variation thereof) you choose
to use is down to what else you want to do with the result (or lack
of). None of the above are either 'right' or 'wrong', they just vary
in terms of applicability to the situation, and, to a large extent,
personal choice.

On Dec 13, 5:01 am, Van [EMAIL PROTECTED] wrote:
 Hello,

 I have a question about using the :contains filter to match the text
 of an anchor tag...

 ul class=list
 lia href=#Text 1/a/li
 lia href=#Text 2/a/li
 ...
 /ul

 I'm trying to use the ul.list selector to run through all anchor tag
 descendants and check their .text() to see if it matches a query.

 $(this:contains()) does not work, and I tried looping through the
 children of ul.list li with an .each and I couldnt quite figure it
 out.  If anyone can help I would appreciate it.  Hopefully this isnt a
 duplicate - i posted this same thing this afternoon but it didnt go
 through correctly.

 Thank you


[jQuery] Re: using :contains() selector with $(this)

2007-12-13 Thread Van

Wizzud,

Thank you for the great response...

$('ul.list a:contains(' + toMatch+ ')')
.each(function(){
  });
fits what I am doing perfectly.  I was so close in my attempts but
never could get the syntax exactly correct with contains.  Your post
helped very much.

Thank you

On Dec 13, 2:51 am, Wizzud [EMAIL PROTECTED] wrote:
 As with anything else there are a number of ways of going about this,
 and which one you use depends a lot on what you want to do with the
 result.
 One thing to point out with your $(this:contains(...)) example is that
 'this' is usually an object variable and ':contains(...)' is a string
 and you can't just tack them together and expect them to work.

 eg.
 // assuming (for example)...
 var toMatch = 'Text 2';

 // then
 var doMatch = $( 'ul.list a:contains(' + toMatch + ')' );
 if( doMatch.length ){
   // got one, and its now held in doMatch

 }else{
   // not found
 }

 // or...
 $( 'ul.list a:contains(' + toMatch + ')' )
 .each(function(){
// do something with the matching anchor ...
   });

 // or...
 $( 'ul.list a' ).filter( ':contains(' + toMatch + ')' )
 .each(function(){
// do something with the matching anchor ...
   });

 //  or...
 $( 'ul.list a' ).each(function(i){
 if( $(this).text() == toMatch ){
   // do something with the matching anchor ...
 }
   });

 //  or...
 var selector = ':contains(' + toMatch + ')';
 $( 'ul.list a' ).each(function(i){
 if( $(this).is(selector) ){
   // do something with the matching anchor ...
 }
   });

 //  or...
 $( 'ul.list a' ).each(function(i){
 if( $(this).text().indexOf(toMatch)  -1){
   // do something with the matching anchor ...
 }
   });

 and there are a number of other variations possible too!
 Note that all the above assume that it is the matching anchor you want
 to end up with. If it's not the anchor you want but the list item
 containing that anchor, then either use parent() on the resulting
 element, or modify the query slightly.

 As I said at the start, which method (or variation thereof) you choose
 to use is down to what else you want to do with the result (or lack
 of). None of the above are either 'right' or 'wrong', they just vary
 in terms of applicability to the situation, and, to a large extent,
 personal choice.

 On Dec 13, 5:01 am, Van [EMAIL PROTECTED] wrote:

  Hello,

  I have a question about using the :contains filter to match the text
  of an anchor tag...

  ul class=list
  lia href=#Text 1/a/li
  lia href=#Text 2/a/li
  ...
  /ul

  I'm trying to use the ul.list selector to run through all anchor tag
  descendants and check their .text() to see if it matches a query.

  $(this:contains()) does not work, and I tried looping through the
  children of ul.list li with an .each and I couldnt quite figure it
  out.  If anyone can help I would appreciate it.  Hopefully this isnt a
  duplicate - i posted this same thing this afternoon but it didnt go
  through correctly.

  Thank you