I need to get neighborhood element value.
HTML is
<div>
<input type='hidden' value='12345'>
<div id='click-this'>Click me</div>
</div>
How can i get "12345" by clicking "click-this" div ?
$('#click-this').click(function() {
/*
* Get siblings element's:
* at this context, input tag element with value 12345
*
*/
})
jQuery next() Method The next() method returns the next sibling element of the selected element. Sibling elements are elements that share the same parent. The DOM tree: This method traverse forward along the next sibling of DOM elements.
The closest() method returns the first ancestor of the selected element. An ancestor is a parent, grandparent, great-grandparent, and so on. The DOM tree: This method traverse upwards from the current element, all the way up to the document's root element (<html>), to find the first ancestor of DOM elements.
The parent() method returns the direct parent element of the selected element. The DOM tree: This method only traverse a single level up the DOM tree. To traverse all the way up to the document's root element (to return grandparents or other ancestors), use the parents() or the parentsUntil() method.
You could do this in multiple ways, but the word neighborhood suggests you could use siblings:
$('#click-this').siblings('input').val();
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