Clearest way to build html elements in jQuery

Templates are great and if you have access to them in your project, I suggest you use them. If you’re using Underscore or Lodash it’s built in. In some cases however, you will need to build HTML in your code whether it’s refactoring or testing. I’ve found that the below format is the clearest to read when that is the requirement.

Note: The HTML spec allows single OR double quotes for attributes in your markup so don’t bother with all the crazy escaping.

this.$fixture = $([
  "<div>",
  "  <div class="js-alert-box"></div>",
  "  <form id='my-form-to-validate'>",
  "    <input id='login-username' name="login-username">",
  "  </form>",
  "</div>"
].join("\n"));

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)