I’ve got 2 ways I can create a <div> using jQuery.
Either:
var div = $("<div></div>");
$("#box").append(div);
Or:
$("#box").append("<div></div>");
What are the drawbacks of using second way other than re-usability?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The first option gives you more flexibilty:
And of course
.html('*')overrides the content while.append('*')doesn’t, but I guess, this wasn’t your question.Another good practice is prefixing your jQuery variables with
$:Is there any specific reason behind using $ with variable in jQuery
Placing quotes around the
"class"property name will make it more compatible with less flexible browsers.