Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can javascript select object not displayed on DOM?

This is a really quick question :)

Just wondering here if its possible for javascript to select objects which aren't part of the DOM... like selecting an :after or :before content created by CSS?

for example...if I have a div and create a box through

div:after{
  content: '.';
  display: block;
  width: 200px;
  height: 200px;
  background: green;
}

I still having difficulties to understand how those elements are created and since they can draw elements on screen but are not part of them DOM, does this mean it's not possible to interact with them?

Cheers

like image 958
zanona Avatar asked Feb 17 '11 16:02

zanona


People also ask

How do you know if an element is present in Dom?

contains DOM API, you can check for the presence of any element in the page (currently in the DOM) quite easily: document. body. contains(YOUR_ELEMENT_HERE);

Is element visible in viewport?

When an element is in the viewport, it appears in the visible part of the screen. If the element is in the viewport, the function returns true . Otherwise, it returns false .


1 Answers

No, you won't be able to interact with them.

They're not part of the DOM, but rather are a manifestation of the style that was assigned.

If you need to add/remove the content, you can use class names.

<div id='myElem' class='withAfter'>some content</div>
div.withAfter:after{
  content: '.';
  display: block;
  width: 200px;
  height: 200px;
  background: green;
}

Then add/remove the class as needed.

like image 80
user113716 Avatar answered Nov 05 '22 16:11

user113716