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
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>
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With