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.
jQuery hide() Method The hide() method hides the selected elements. Tip: This is similar to the CSS property display:none.
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.
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.
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);
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
.
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