views:

141

answers:

3

Thanks for looking, all helpful answers are voted up. This is my markup. I'm trying to find 2 consecutive divs cmk1 and cmk2 with the content RIGHT and HERE in consecutive order.

  • div id 1 shouldn't match because there's a right but not here.
  • div id 3 shouldn't match because there's a here but no right.
  • I'm trying to find something that looks like div id 2 where right is followed by here. Also the text has to be exact: <div>more than right</div> should not match even though it contains the word right

What's the most efficient way to do this?

Update: I just had a thought, I could find each class=cmk1. if it matches RIGHT, I could select its next (cmk2) and if it matches also, that's what I'm looking for. But how do I do this while loop in jquery? and most importantly how do I exit out of it?

<div class="sep" id="1">
  <div class="cmk1">right</div>
  <div class="cmk2">valc</div>
  <div class="opp">vald</div>   
  <a class="go">Go</a>      
</div>
<div class="clear">
<div class="sep" id="12">
  <div class="cmk1">RIGHT</div>
  <div class="cmk2">HERE</div>
  <div class="opp">vala</div>   
  <a class="go">Go</a>      
</div>
<div class="clear">
<div class="sep" id="59">
  <div class="cmk1">vale</div>
  <div class="cmk2">valf</div>
  <div class="opp">here</div>   
  <a class="go">Go</a>      
</div>
<div class="clear">
+1  A: 
$(':contains(RIGHT) :contains(HERE)')
powtac
Actually it has to not just contain, but the full thing be equal. For example if I'm looking for the string `partial`, `<div>partial</div>` should match true, but `<div>Some partial content</div>` should not match.
Chris
+1  A: 

Try:

$(':contains(right), :contains(here)')
    .filter( function() {
        return ( $(this).text() == 'right' && $(this).next().is(':contains(here)') ) ||
            ( $(this).text() == 'here' && $(this).prev().is(':contains(right)') );
    } );

The only drawback is this is case sensitive.

Tinister
+3  A: 
$('div.sep > div').each(function() {
    if($(this).text() == 'RIGHT') {
        if($(this).next('div').text() == 'HERE') {
            alert('Values occur consecutively in div id: ' +  $(this).parent().attr('id'));
            return false;            
        }
    }
});

I've basically looped over all the child divs of the each .sep div and tested for the first word. If it is matched, next can be used to determine if the next div contains the second word.

karim79
This goes through each one. How do I break once I've found the right one so I don't have to keep spinning my wheels now that I already found it :)
Chris
@Chris - I've edited, simply return false to break out of the loop.
karim79
Nicely done, but I think `$('.sep .cmk1')` be a more appropriate selector (less nodes selected and not tag dependent).
Justin Johnson
@Justin Johnson - I agree with you, I just did not know how much to narrow down the selection as I was unsure as to exactly what the OP was looking for (e.g. could the first word be in .cmk2 ?). Anyway, your comment is most helpful.
karim79
@karim79, What's the diff between .text() and .html() by the way? I always used .html(). Thanks for the help. Looks like this will work just fine.
Chris
@chris - html() gives you the raw html inside the specified tag, while text() will give you the combined text content all the contained HTML.
karim79