MarkL
MarkL

Reputation: 109

Ned the correct jQuery syntax to add an id to a Div

jQuery is creating a div like this

document.write("<div class='post' id=''>");
document.write(postcontent);
document.write(json.feed.entry[i].id.$t);
document.write('</div>');

But I want to move (json.feed.entry[i].id.$t) into the id of the div like this

document.write("<div class='post' id='(json.feed.entry[i].id.$t)'>");
document.write(postcontent);
document.write('</div>');

Cant get the syntax right as the above example does not work..

Upvotes: 0

Views: 33

Answers (2)

Rahul Desai
Rahul Desai

Reputation: 15501

You arent concatenating correctly. Do it this way:

document.write("<div class='post' id='"+(json.feed.entry[i].id.$t)+"'>");

Upvotes: 1

Milind Anantwar
Milind Anantwar

Reputation: 82241

You have not handled the quotes correctly while setting id value. use:

document.write("<div class='post' id='"+json.feed.entry[i].id.$t+"'>");
document.write(postcontent);
document.write('</div>');

You can also narrow down document.write to be used only once:

document.write("<div class='post' id='" + json.feed.entry[i].id.$t + "'>" + postcontent + "</div>");

Upvotes: 1

Related Questions