1

here is the extract of my code

 $("#temp").append('<div class="dynamic">' );
    $("#temp").append(a[i]);
    $("#temp").append("</div>");

this content is inside a for loop and dynamically add elements to div with id temp.

what i think it should produce is

<div id="temp">

.....
<div class="dynamic">
Education //value of a[i]
</div>
....


</div>

but what exactly it produces it

<div id="temp">

    <div class="dynamic"></div>

    Education

as confirmed from firebug. Because of which my css styling just breaks up.

Can you look into it? Thank you

1
  • a is an array which i have created earlier in jquery code. Commented Oct 1, 2013 at 7:23

3 Answers 3

7

Creating elements with jQuery should be done like this :

$('<div />', {'class': 'dynamic', text: a[i]}).appendTo('#temp');
Sign up to request clarification or add additional context in comments.

3 Comments

@Wanna_be_Coder if you got correct solution then pick it as an answer
Why is class enclosed in quotes, but text is not? Can it be either way? Or is it just because class is a keyword? Any example of this syntax with slightly more complex markup, such as with additional child elements?
@JonathanWood - Yes, class is a reserved keyword in javascript, that's why it's quoted. Here's an example of something a little more complicated -> jsfiddle.net/421qd4n7/1
0

try this in loop, i guess your problem is solved...

var data = "<div class='dynamic'>" + a[i] + "</div>";
$("#temp").append(data);

Comments

0

Use

var data = '<div class="dynamic">' + a[i] + '</div>';
$("#temp").append(data);

As when you append $("#temp").append('<div class="dynamic">' ); a div is created and closed.

and when you append $("#temp").append(a[i]); it is appended after div

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.