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"
});
Sign up to request clarification or add additional context in comments.

Comments

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"
});

Comments

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.