Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get only visible element using pure javascript

Tags:

javascript

I have elements like below

<div class="one">send Message</div>

<div class="one">send Message</div>

<div class="one">send Message</div>

I have a web page where there is send Message buttons like above, in which only one button is visible at a time.Other two buttons are hidden via some javascript codes.So for example if 2nd button is visible , I should be able to get only that element.

So my code will be something like

document.querySelector(".one:visible");

In jquery the code is $(".one:visible"); , which works fine , But I need to know how to do this via pure javascript.

like image 958
Vishnu Avatar asked Jun 18 '17 05:06

Vishnu


People also ask

How do you make an element visible in Javascript?

To show an element, set the style display property to “block”. document. getElementById("element"). style.

How can we check element visibility?

We can do the following to check an element's visibility in the viewport: Get the bounding rect of the DOM element using the getBoundingClientRect . This method returns an object with an element's width, height, and position relative to the viewport.

Which jQuery filter can be used to check if element is visible?

Answer: Use the jQuery :visible Selector You can use the jQuery :visible selector to check whether an element is visible in the layout or not. This selector will also select the elements with visibility: hidden; or opacity: 0; , because they preserve space in the layout even they are not visible to the eye.


1 Answers

If you're using the hidden attribute :

document.querySelector(".one:not([hidden])");
like image 168
Mojimi Avatar answered Oct 31 '22 13:10

Mojimi