Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to select text in jquery context from a sibling element context?

For instance, the code is:

<ul>
   <li><strong>This is a list header.</strong> And I want this text to disappear because this is not a list header!</li>
</ul>

And the JavaScript code is:

$('ul li').hide();
$('ul li strong').fadeIn();

What I am trying to achieve is to hide the text that is not inside <strong>.

like image 662
rationalboss Avatar asked Aug 04 '14 06:08

rationalboss


People also ask

What does siblings do in jQuery?

The siblings() is an inbuilt method in jQuery which is used to find all siblings elements of the selected element. The siblings are those having same parent element in DOM Tree.

How do I get just the text from HTML in jQuery?

The best way is to . clone() your object, . remove() all of its . children() , then go back to the object using .

Which function in jQuery is used to grab a previous sibling element?

jQuery prev() Method The prev() method returns the previous sibling element of the selected element. Sibling elements are elements that share the same parent.

How do I select something in jQuery?

The select() method is an inbuilt method in jQuery which is used when some letters or words are selected (or marked) in a text area or a text field. Syntax: $(selector). select(function);


1 Answers

Try to wrap that text inside a span and do,

HTML:

<ul>
   <li>
     <strong>This is a list header.</strong>
     <span>And I want this text to disappear because this is not a list header!</span>
   </li>
</ul>

JS:

$('ul li >').hide().filter('strong').fadeIn();

DEMO

like image 199
Rajaprabhu Aravindasamy Avatar answered Oct 13 '22 09:10

Rajaprabhu Aravindasamy