Reputation: 20834
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
Upvotes: 5
Views: 2445
Reputation: 3780
You can do this with jQuery without making extra functions and stuff like this...
$('.click').on('click', function(){
$('.change-on-click', $(this).parents('div')).css('color', 'red');
});
<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>
Upvotes: 0
Reputation: 24638
Is this what you're looking for? ( Locate the target element via a while loop )
$('.click').on('click', function() {
var coc = $(this);
while( !coc.prev().is( '.change-on-click' ) ) {
coc = coc.parent();
}
coc.prev().css('color','red');
});
$('.click').on('click', function() {
var coc = $(this);
while( !coc.prev().is( '.change-on-click' ) ) {
coc = coc.parent();
}
coc.prev().css('color','red');
});
<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>
OR (Using a jQuery plugin)
$('p.click').on('click', function(){
$(this).changeOnClick();
});
$.fn.changeOnClick = function() {
return this.each(function() {
var near = $(this);
while( !near.prev().is('.change-on-click') ) {
near = near.parent();
}
near.prev().css('color','red');
});
}
<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>
UPDATE
The following version should work for cases where the target element is before or after the clicked element:
$('.click').on('click', function() {
var coc = $(this);
while( !coc.prev().is( '.change-on-click' ) && !coc.next().is( '.change-on-click' ) ) {
coc = coc.parent();
}
coc.prev().add( coc.next() ).filter( '.change-on-click' ).css('color','red');
});
Upvotes: 1
Reputation: 82287
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>
Upvotes: 3
Reputation: 1661
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/
Upvotes: 2