0

I create and append a div with an id:

classyDiv = document.createElement('div');
classyDiv.setAttribute('id', 'classyDivID');

parentDiv = document.getElementById('parentDivID');
parentDiv.appendChild(classyDiv);

I now want to attach this css class to the child div:

.igv-top-left-width {
    position: absolute;
    top: 128px;
    left: 256px;
    width: 512px;

}

I use jQuery to assign the css class:

$('#classyDivID').addClass('.igv-top-left-width');
console.log($('#classyDivID').css('left'));

When I echo the css class values I get complete nonsense values. For left I get auto. Huh? Whah?. As if I am referring to some other random css class. What am I doing wrong here?

1
  • 1
    Remove the period. addClass expects a class name, not a selector. What you're currently doing is equivalent to <div class=".class-name">. Commented Mar 4, 2014 at 2:31

1 Answer 1

3

Try to change:

$('#classyDivID').addClass('.igv-top-left-width');

to:

$('#classyDivID').addClass('igv-top-left-width');

You don't need the . to add the class here

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

3 Comments

No joy. var cssClassName="igvTopLeftWidth"; $('#classyDivID').addClass(cssClassName); and console.log("top") echos the word auto. console.log("width") echos 1184px. Sigh.
Can you create a fiddle to replicate your problem?
Got it, it works fine. Here is the fiddle: jsfiddle.net/dugla/74eeC. The problem had to do with the fact I was running inside a unit test (jQuery QUnit). I'll give this one to you Felix for the suggestion. Cheers.

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.