Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery what instead $(this).parent().children()

Tags:

Just a quick example:

<p>     <span class="example"></span>     <input type="text" name="e_name" id="e_id /> </p> <script type="text/javascript">     $('input').click(function(){         $(this).parent().children('span').text('Suprise!');     } </script> 

What can I use instead parent().children()?

I think it's a bit inelegant piece of code. Is any function i.e : $(this).fun('span').text('just better'); ??

like image 420
Mr Sooul Avatar asked May 27 '11 17:05

Mr Sooul


People also ask

What is the use of parent () and child () method in jQuery?

parent: Using this, the parent element will be selected. child: Using this, the direct child element of the specified parent element will be selected.

Can we get the children element from the parent element using jQuery?

jQuery children() method is used to get the direct children of the selected HTML element. You can use children() method to traverse through the child elements of the selected parent element.

How do you choose a child from a parent?

The ("parent > child") selector selects all elements that are a direct child of the specified element.

What does parent () do 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

$(this).siblings('span').text('Suprise!'); 
like image 65
Christopher Armstrong Avatar answered Nov 24 '22 07:11

Christopher Armstrong