Can css affect elements added by document.write?

If I have this css

div.myContainer img.noCampaign { display:none; }

      

and this html / javascript

<div class="myContainer">
  <script>document.write('<img class="noCampaign" src="whatever.jpg" />');</script>
</div>

      

I can't get the img element to disappear?

Is there no way to access the img element via css when dynamically adding elements with javascript document.write?

/ T

+2


source to share


4 answers


The CSS styles create js-generated HTML elements - in fact, your example code works exactly as expected in my tests. (After adding to the element <script>

, anyway)



I suspect there is another problem in your code that prevents it from behaving the way you like. If you edit the question with the code you actually used, we can help you find the problem. It's probably just a syntax or a priority error in there.

+3


source


You can add children to your document without using document.write. Add the image element as a child to the div.

Using



document.createElement

0


source


yes everything that is generated (static or dynamic) depends on any css (external, inline or inline)

0


source


Did the URL help you?

document.write('<span style="color:#FFFFFF;"> <img src="www.example.com/img.jpg" /> some text and a <a href="http://example.com/" title="example">link</a>');

      

-1


source







All Articles