1

I want select DOMs using jQuery.

HTML code

<li>
  <span class="node>
    <span class="con"></span>
    <span class="check"></span>
    <img>
    <a title="high">abc</a>
  </span>
</li>
<li>
  <span class="node>
    <span class="con"></span>
    <span class="check"></span>
    <img>
    <a title="high">def</a>
  </span>
</li>
<li>
  <span class="node>
    <span class="con"></span>
    <span class="check"></span>
    <img>
    <a title="low">zwc</a>
  </span>
</li>

I want select "check" class span DOMs what have "a tag" has "high" title in same level.

I tried to click that DOMs using this query:

$('a[title="high"]').each(function() {
  $(this).prev().prev().click();
})

but this code have only first match value.

How can I select all DOMs?


My mistake. I want "check" not "con". Thanks.

The point of this question is "all" DOMs what have "high" title attr should be clicked.

5 Answers 5

1

you can use .closest().find()

$('a[title="high"]').each(function() {
  $(this).closest('.node').find('.con').click();
})

Working Demo

Sign up to request clarification or add additional context in comments.

3 Comments

This code is working but return only first match. All DOMs what have a "high" title should be clicked.
@RobertMoon did you check the demo?? Its already returned all not just first
Oh yes, the demo work well. I think there is a code block loop after click event. I'll find that. Thanks.
1

You can use filter() to filter out elements from a set based on condition.

// Get all spans having class `con`
$('span.con').filter(function () {
    // If this element has anchor element sibling with `high` as Title
    // then return true, else return false
    return $(this).siblings('a[title="high"]').length;
}).css('color', 'green');

$('span.con').filter(function() {
  return $(this).siblings('a[title="high"]').length;
}).css('color', 'green');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
  <li>
    <span class="node">
    <span class="con">First</span>
    <span class="check"></span>
    <img>
    <a title="high">abc</a>
    </span>
  </li>
  <li>
    <span class="node">
    <span class="con">Second</span>
    <span class="check"></span>
    <img>
    <a title="high">def</a>
    </span>
  </li>
  <li>
    <span class="node">
    <span class="con">Third</span>
    <span class="check"></span>
    <img>
    <a title="low">zwc</a>
    </span>
  </li>
</ul>

Comments

1

Instead of .prev().prev() use siblings() to find the items in the same level and filter with .con.

$('a[title="high"]').each(function() {
  $(this).siblings('.con').click();
});

If you want to know why actually your code is not working, you called prev() two times. By first one you select img, the second one will select .check. So you need to call prev() third time to reach .con. This is always confusing and may break anytime if you add more element in between. That's why this approach is not recommended.

1 Comment

This code return only first match. Because of my fault, your comment is right but this code is not work after I fixed that point.
1

As CSS and jQuery don't have previous sibling selector, you can use General sibling selector.

You can use General sibling selectors to select elements having specific attribute and then use jQuery's sibling() to select span.con.

$('span.check ~ a[title="high"]') // Select all anchors having `title` as "high"
                                // Which are next siblings of `span` having class of `check`
    .siblings('span.check') // Select sibling `span` having class `con`
    .css('color', 'green');

The selector $('span.check ~ a[title="high"]') will select anchor elements, to get the span, use siblings.

$('span.check ~ a[title="high"]')
  .siblings('span.check')
  .each(function() {
    $(this).css('color', 'green').click();
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
  <li>
    <span class="node">
    <span class="con">First</span>
    <span class="check">First Check</span>
    <img>
    <a title="high">abc</a>
    </span>
  </li>
  <li>
    <span class="node">
    <span class="con">Second</span>
    <span class="check">Second Check</span>
    <img>
    <a title="high">def</a>
    </span>
  </li>
  <li>
    <span class="node">
    <span class="con">Third</span>
    <span class="check">Third Check</span>
    <img>
    <a title="low">zwc</a>
    </span>
  </li>
</ul>

3 Comments

As your explain, I wrote a code like this : $('span.check ~ a[title="high"]').siblings('span.check').click(); but this code check only first match. All DOMs should be checked.
@RobertMoon You can iterate over the collection using each and use .click inside it.
Oh yes, I did. $('span.node ~ a[title="high"]').each(function () { $(this).siblings('span.check').click(); }); But not work.
1

Actually your code is working fine. See I tried this, HTML :

<li>
<span class="node>
<span class="con"></span>
<span class="check" onclick="console.log('span :'+1);"></span>
<img onclick="console.log('img :'+1);">
<a id = "m1" title="high" onclick="console.log('a :'+1);">abc</a>
</span>
</li>
<li>
<span class="node>
<span class="con"></span>
<span class="check" onclick="console.log('span :'+2);"></span>
<img onclick="console.log('img :'+2);">
<a id = "m2" title="high" onclick="console.log('a : '+2);">def</a>
</span>
</li>
<li>
<span class="node>
<span class="con"></span>
<span class="check" onclick="console.log('span : low');"></span>
<img onclick="console.log('img : low');">
<a id = "m3" title="low" onclick="console.log('low');">zwc</a>
</span>
</li>

JS:

$(function(){
$('a[title="high"]').each(function() {
        // if you want to click all <a>
        $(this).click();
        // if you want to click all <img>
        $(this).prev().click();
        // if you want to click all <span> having class check
        $(this).prev().prev().click();
    });});

Console:

a :1
img :1
span :1
a : 2
img :2
span :2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.