Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - get neighborhood element

Tags:

jquery

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 
     *
     */  

})
like image 800
Qiao Avatar asked Apr 01 '10 05:04

Qiao


People also ask

How can I get next sibling in jQuery?

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.

How do I get the closest div using jQuery?

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.

How do I find a specific parent in jQuery?

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.


1 Answers

You could do this in multiple ways, but the word neighborhood suggests you could use siblings:

$('#click-this').siblings('input').val();
like image 90
John McCollum Avatar answered Oct 11 '22 17:10

John McCollum