Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select nearest element by class on the same(or not) level

How to select the nearest element with a specific class that is or may not be on the same level as the clickable element?

For example, lets says that this is the output:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
    <div>
        <p class="change-on-click">A</p>
        <p class="click">Click on this will change A</p>
    </div>
</div>

<div>
    <div>
        <p class="change-on-click">B</p>
        <div>
            <p class="click">Click on this will change B</p>
        </div>
    </div>
</div>

<div>
    <div>
        <p class="change-on-click">C</p>
        <div>
            <div>
                <p class="click">Click on this will change C</p>
            </div>
        </div>
    </div>
</div>

<div>
    <div>
        <div>
            <div>
                <p class="click">Click on this will change D</p>
            </div>
            <p class="change-on-click">D</p>
        </div>
    </div>
</div>

<div>
    <div>
        <div>
            <div>
                <p class="click">Click on this will change E</p>
            </div>
        </div>
    </div>
    <p class="change-on-click">E</p>
</div>

I want when I click on p.click that it will find the nearest p.change-on-click element.


Just like this, but unfortunately I can't change the HTML.

$('.click').on('click', function(){
    $(this).closest('.parent').find('.change-on-click').css('color', 'red');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="parent">
    <div>
        <p class="change-on-click">A</p>
        <p class="click">Click on this will change A</p>
    </div>
</div>

<div class="parent">
    <div>
        <p class="change-on-click">B</p>
        <div>
            <p class="click">Click on this will change B</p>
        </div>
    </div>
</div>

<div class="parent">
    <div>
        <p class="change-on-click">C</p>
        <div>
            <div>
                <p class="click">Click on this will change C</p>
            </div>
        </div>
    </div>
</div>

<div class="parent">
    <div>
        <div>
            <div>
                <p class="click">Click on this will change D</p>
            </div>
            <p class="change-on-click">D</p>
        </div>
    </div>
</div>

<div class="parent">
    <div>
        <div>
            <div>
                <p class="click">Click on this will change E</p>
            </div>
        </div>
    </div>
    <p class="change-on-click">E</p>
</div>

Here's a fiddle with the second situation

like image 559
Vucko Avatar asked Dec 08 '14 21:12

Vucko


2 Answers

This is going to require a custom approach since the jQuery API does not support that type of searching. Basically, just iterate through the parent nodes until you find the click-on-change element.

$('.click').click(function(){
  var node = this;
  while(node != document.body){
   var target = $(node).find('.change-on-click')
   if( target.length == 0 ){
    node = node.parentNode;
   }else{
    target.css('color','red');
    return;
   }
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div>
    <div>
        <p class="change-on-click">A</p>
        <p class="click">Click on this will change A</p>
    </div>
</div>

<div>
    <div>
        <p class="change-on-click">B</p>
        <div>
            <p class="click">Click on this will change B</p>
        </div>
    </div>
</div>

<div>
    <div>
        <p class="change-on-click">C</p>
        <div>
            <div>
                <p class="click">Click on this will change C</p>
            </div>
        </div>
    </div>
</div>
like image 158
Travis J Avatar answered Oct 14 '22 15:10

Travis J


You can do it this way:

$('p.click').each(function(i){
    $(this).on("click", function(){$('.change-on-click').eq(i).css('color', 'red');});
});

http://jsfiddle.net/4ofajqxa/1/

like image 29
Verhaeren Avatar answered Oct 14 '22 14:10

Verhaeren