0

I have seen a few different methods for adding classes to dynamically created elements using JQuery.

I'm most familiar with

$("<div />").addClass("class1 class2");

however I have seen a lot of

$("<div />", {
  class : "class1 class2"
});

When I test out the second method in a Fiddle I can see both class1 and class2 are applied.

however, when applied to what i'm working on

// this does not work
var b = $("<div id='tweetBox' />", {
    class : "triangle-right right"
});

// this works
var b = $("<div id='tweetBox' />").addClass("triangle-right right");
0

2 Answers 2

1

you can't mix and match.

try:

var b = $("<div />", {
    id: "tweetBox",
    class : "triangle-right right"
});
0
0
// this does not work
var b = $("<div id='tweetBox' />", {
    class : "triangle-right right"
});

Doesn't work because there is an attribute on the element you are creating.

This will work
var b = $("<div />", {
    id: 'tweetBox',
    class : "triangle-right right"
});

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.