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'); ??
parent: Using this, the parent element will be selected. child: Using this, the direct child element of the specified parent element will be selected.
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.
The ("parent > child") selector selects all elements that are a direct child of the specified element.
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.
$(this).siblings('span').text('Suprise!');
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