views:

235

answers:

5

One nice thing with jQuery is that the syntax (such as when chaining) allows your script to span multiple line breaks for ease of formatting and readability.

Is there an equivalent/preferred method when one is adding a large string of HTML?

For instance, it'd be nice to do something like this:

 $("#theObject")
    .doSomething()
    .append("
        <div>
             <div>
                  Hello World
             </div>
        </div>
    ")

That doesn't work but is there something akin to that to make it easier to read HTML being formatted within a jQuery script?

+2  A: 
 $("#theObject")
.doSomething()
.append(
    "<div>"
         +"<div>"
              +"Hello World"
         +"</div>"
    +"</div>"
);

It'd be best practice to load your .append() content as a var, like so:

var append_me =
     "<div>"
         +"<div>"
              +"Hello World"
         +"</div>"
    +"</div>";

$("#theObject").doSomething().append(append_me);
iamkoa
iamkoa...nice! Simple and obvious in hindsight, but something I hadn't considered.
DA
+1  A: 

Add a \ at the end of each line

$("#theObject").doSomething()
.append("
    <div>\
         <div>\
              Hello World\
         </div>\
    </div>\
")
Geuis
What, did you write your own interpreter or something? This certainly is not valid Javascript.
Josh Stodola
This appears to work!? So, is this jQuery centric syntax?
DA
I'd argue that it's valid. It works in almost any other programming language, as well.
S Pangborn
Yeah, that's valid javascript. I think in the past I've had to put spaces before the slashes to get it to work in all the browsers
Dan F
This fails for me in IE as "unterminated string constant"
Josh Stodola
@josh I ran into that too. The catch is that you can't have any characters (including spaces) AFTER the backslash.
DA
A: 
var str  = '<div>';
str += '<div>';
str += 'Hello World';

$("#theObject")
    .doSomething()
    .append(str)
Dhana
That was my first thought. Not perfect, but more readable than cramming everything into one line. Is there a performance hit, though? Does concatenating a declared variable multiple times affect performance in anyway?
DA
It shouldn't in current browsers. There used to be a performance hit before, and the best way then was to push the strings into an array and joining them in the end. But I've done a few tests though and most browsers now optimize string concatenation.
Dhana
+1  A: 

There is no elegant way of representing a nice HTML tree structure in Javascript using strings. You will simply have to use a bunch of concatenation, which is always going to be ugly, and might I add that it performs terribly. This is what I do:

  1. Write my markup in my favorite editor with all the whitespace I want
  2. Save it in the same location as your JS file, with the same name + ".txt" as the extension
  3. My editor has find-and-replace with RegEx, so I do a replace-all with something like >[\s]*<
  4. Copy the one-lined result over to my Javascript file as a string variable
  5. Undo the replace on the .txt file

This implies, of course, that any time you change the markup you will need to re-do the replace and copy it over. I got into that habit pretty quickly, though. Additionally, jQuery will be able to process this faster because there will be less characters to parse.

Josh Stodola
Thanks, Josh. Yea, that would certainly work but one of the goals is to make this readable for other folks. Ideally, the line breaks and indenting would preserved within the .js block for future maintenance by others.
DA
+1  A: 

If you're using jQuery, and your string literals are going to be html elements, you could also just create those elements directly on the page and hide them, then refer to those elements in the DOM instead of typing them out as strings. For example, you could put this on your page:

<div id="myContent" style="display: none;">
    <div>
        <div>
            Hello World
        </div>
    </div>
</div>

And then rewrite your code like this:

$("#theObject")
.doSomething()
.append(
    $('#myContent').html();
);

Alternatively you could append the actual DOM element itself if you only need to reference it the one time, instead of copying its contents every time.

Rudism
Actually I guess that wouldn't be limited to just HTML elements... obviously you could put anything at all into #myContent.
Rudism
@Rudism: very true. That's useful at times.
DA
Yes -- really useful to use the DOM like this where possible. The major exception might be when you are writing library code that uses long strings and gets called on many pages of a site. The library only gets loaded once (assuming correct caching policy), so it is more efficient to put any long strings there than in the page content itself.
Ben