1

I create an element by Javascript and apply it to another element, I also have a CSS File with styles for this new element, I check the code, the new element exists and has the given ID and class name, but my CSS is not used. The code looks like this:

var mainDiv=document.createElement("DIV");
mainDiv.id="ambiVid_controls";
mainDiv.className="ambiVid_controls";

Conts.parentNode.appendChild(mainDiv);

And the CSS:

.ambiVid_controls{
    position:absolute !important;
    bottom:15px ;
    width:60% !important;
    height:50px !important;
    margin-left:50% !important;
    background-color:#333 !important;
    z-index:1000 !important;

}

Anyone have any ideas why this gets ignored or is there any known issue with ApplyChild and CSS?

The browser is Safari, if this matters.

7
  • 3
    "Amyome am odea why this igets ignored" I'm sorry, but seriously, couldn't you have done just a bit of proofreading, either in the edit area or the proofing area? Commented Jul 7, 2011 at 22:04
  • 1
    I think we're going to need more code. This looks like it should work. Commented Jul 7, 2011 at 22:06
  • 1
    I concur. At the risk of sounding useless, why should I expend any effort on an answer if there was no effort expended on the question? Commented Jul 7, 2011 at 22:07
  • What is Conts and is it reachable from document.body? Commented Jul 7, 2011 at 22:12
  • 1
    Why do you have both an ID and a class name with the same name? Use one or the other... Commented Jul 7, 2011 at 22:15

2 Answers 2

1

Maybe the div is not being added correctly. Because it works here http://jsfiddle.net/XDYbN/

Sign up to request clarification or add additional context in comments.

Comments

0

You would not have to add the className property if your CSS was defined thusly:

#ambiVid_controls
{
    position:absolute !important;
    bottom:15px ;
    width:60% !important;
    height:50px !important;
    margin-left:50% !important;
    background-color:#333 !important;
    z-index:1000 !important;    
}

This, of course, would not work if you are adding multiple elements with that ID value.

Edit: Instead of: Conts.parentNode.appendChild(mainDiv); .. and if Conts is the ID of an existing element in the DOM, like say a DIV, just do Conts.appendChild(mainDiv);

2 Comments

While true that the OP could use the ID instead, that doesn't address the OP's question. In the question, the OP is clearly adding an element with the relevant class name, which matches the CSS shown. Changing the CSS so it matches the element's ID instead isn't going to help.
So currently i tried all your suggestions with no luck, i removed the ID from JS, i added also a selector to the CSS for the Parent, i check Safari Inspector, everything was added correcly, the Element was created and is at the wanted position but the CSS was not used, the CSS Class was not applied, the Element uses just default CSS or gained by Parents...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.