Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In jQuery, how can I select a hidden element?

How can I select the <span> where display is set to none in the below code?

<p id="p1"> <span id="test1" style="display:none">test1</span>  <span id="test2" >test2</span>  </p> 

I can select the <span> whose ID is "test1" by using $("span[id='test1']"), but it does not work when I use $("span[style='display:none']").

Is there any method to get this element at a time?

Thanks a lot.

like image 905
Kirk Avatar asked Mar 25 '09 02:03

Kirk


People also ask

What jQuery method is used to hide selected elements?

jQuery hide() Method The hide() method hides the selected elements. Tip: This is similar to the CSS property display:none.

Can you click hidden element from Javascript?

In case an element is a part of the form tag, it can be hidden by setting the attribute type to the value hidden. Selenium by default cannot handle hidden elements and throws ElementNotVisibleException while working with them. Javascript Executor is used to handle hidden elements on the page.

How do you select visible elements?

To achieve the best performance when using :visible to select elements, first select the elements using a pure CSS selector, then use . filter(":visible") . Using this selector heavily can have performance implications, as it may force the browser to re-render the page before it can determine visibility.

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

You are looking for the :hidden selector

Please note that the proper way of selecting an element by ID is simply:

$("#test1"); 

Doing it the way you are doing is making jQuery do unnecessary parsing and is much slower.

If you want to select #test1 only if it is hidden, you do this:

$("#test1:hidden"); 

If you wanted to select all <span> elements that are hidden under #p1, you do this:

$("span:hidden", "#p1"); 

As noted in the comments, the opposite of this selector is the :visible selector:

$("span:visible", "#p1"); 

Would then select any visible <span> elements in the element #p1.

like image 110
Paolo Bergantino Avatar answered Sep 23 '22 08:09

Paolo Bergantino