I am trying to output html with jQuery.
$('body').append('
<div>
<div>
<div></div>
</div>
</div>
');
I want to indent the code in this way for higher readability. It works for php but not in JS, any ideas? I use notepad++ as editor.
I am trying to output html with jQuery.
$('body').append('
<div>
<div>
<div></div>
</div>
</div>
');
I want to indent the code in this way for higher readability. It works for php but not in JS, any ideas? I use notepad++ as editor.
You should be using a templating system for that. No matter how much indentation you use, if you mix to much HTML and Javascript you will end-up will spaghetti code.
<script type="text/template" id="my-template">
<div>
<div>
<div></div>
</div>
</div>
</script>
$('body').append($('#my-template').html());
If you need more control over your template rendering, you need to use a library. I recommend underscore.js, its not the best templating engine for javascript, but it's very lightweight and comes with a great set of helper functions.
$('body').append('\n\
<div>\n\
<div>\n\
<div></div>\n\
</div>\n\
</div>\n\
');
\n is to insert a newline character in your string (so it’ll get outputted in your body element)\ is Javascript's line continuation character, so you can keep your JS code indented without having to add + '' everywhere.