Reputation: 5042
Suppose that I want to select the nearest target element from startFromThis
(only the element that come after startFromThis
. the target might be a child of any unnamed or unclassed element. Is there anyway to select this.
Sorry, i would like to edit startFromThis to be an ID
<div id="startFromThis"><div>
<div>
<span class="target"></span>
<div>
Upvotes: 0
Views: 58
Reputation: 167172
You can use the .closest()
function for this:
$(document).ready(function(){
$('.startFromThis').closest('.target').html('Selected!');
});
Check out the documentation here: http://api.jquery.com/closest/
Upvotes: 1
Reputation: 1123
$('#startFromThis').closest('target element')
assuming startFromThis
is an id.
Upvotes: 3