1

I have a lot of JavaScript code that creates HTML. For example:

function Reply(postId) {
    if (!document.getElementById("reply" + postId)) {
        $("#root" + postId).after("<div id='reply" + postId + "'><textarea cols='70' rows='8' style='margin-bottom: 10px'></textarea><br/><span style='margin-bottom: 30px'><input type='button' value='Submit' /><input type='button' value='Cancel' onclick='$(\"#reply" + postId + "\").remove();'/></span></div>");
    }
}

How can I use jQuery to make this more readable? What's the best way to go about making new elements in JavaScript? Can I see an example?

0

6 Answers 6

2

The standard way is not to create html and then appending or prepending it but to create dom elements and adding them.

Please read this wonderful article on innerHTML vs DOM. by Tim Scarfe. It's very well written and and points and counter points.

You can read more on DOM here. and a Lot of information at the Gecko DOM Reference..

Sorry for only pasting links but it's a lot to go through.

If you want a quickstart. Look at this part of the Gecko DOM Reference. where they talk about createElement. Which is the magical method you're looking for.

Sign up to request clarification or add additional context in comments.

8 Comments

Creating DOM elements on the fly can be a real pain tho. I like templating (stackoverflow.com/questions/170168/jquery-templating-engines) when adding new markup on the fly.
Is that because it's faster to create DOM elements? I was under the impression that browsers optimize HTML rendering enough that innerHTML (or wrappers thereof) were faster. or is it to do with coding practices?
Shudder - That looks like a really nasty hack for no good reason
+1 for it being a complete pain in the ass to create even mildly complex HTML fragments with the DOM. Generate the HTML serverside and slap it in with innerHTML
Read up on DOM and why this is the standard way to do this. And why innerHTML is something Microsoft created to "bypass" working with the DOM in the correct way. (And that other browsers had to comply since many people were wrongly using innerHTML)
|
0

First of all you need to know where you want to append the HTML, here you'll see the documentation: for "manipulation": here.

So lets say you want to create a paragraph inside a DIV with the ID "insideme".

$("#insideme").append("<p>I'm adding this</p>");

That is for creation, now lets say you want to insert an existent DIV:

$("#insideme").append($("div#existent"));

Check the documentation, there you'll find every function that is used to add before, after, inside, etc.

Hope it helped you.

Comments

0

If you want to fully use jquery functions, see if the following works (I have not tested it). Howerver, I would actually advise against this since I understand that direct HTML-string parsing/rendering is optimized a lot faster than a bunch of js function calls.

function Reply(postId) {
  var rid = "reply"+postId;
  if (!$('#'+rid)) {
    $('#root'+postID)
      .append("<div/>").attr('id',rid)
        .append("<textarea/>").attr({'cols',70,'row',8,'style','margin-bottom: 10px'})
        .after("<br/>")
        .after("<span />").attr('style','margin-bottom: 30px');
          .append("<input type='button' value='Submit' />")
          .after("<input type='button' value='Cancel' />")
          .onclick(function(){
             $("#"+rid).remove();
          });
  }
}

Comments

0

Two ways:

  • Improve the formatting of that string
  • Install event handlers programatically

Eg:

function Reply(postId) {
    if (!document.getElementById("reply" + postId)) {
        // Generate HTML
        var html = "<div id='reply" + postId + "'>";
        html += "<textarea cols='70' rows='8' style='margin-bottom: 10px'></textarea><br/>";
        html += "<span style='margin-bottom: 30px'>";
        html += "<input type='button' value='Submit' />";
        html += "<input class='cancelButton' type='button' value='Cancel' />";
        html += "</span>";
        html += "</div>";

        // Insert into DOM
        $("#root" + postId).after(html);

        // Install event handlers
        $("#root .cancelButton").bind("click", function() {
            $("#reply" + postId).remove();
        });
    }
}

Using the DOM is methods directly is specifically discouraged by jQuery since it's so slow.

Update: As Chris says, move those styles to a CSS file to further tidy this up.

Comments

0

For such things I always use EJS.

http://embeddedjs.com/

Remove the angle brackets from your jQuery.

$( ".cancel" ).live( 'click', function() { $( this ).remove(); });

function reply( postId ) {
    if ( $( "#reply" + postId ).size() == 0 ) {
        var context = { postId: postId };
        $( "#root" + postId ).after(new EJS({ url: '/template.ejs' }).render( context ));
    }
}

Put them in a template with all their pointy little friends.

<div id="reply<%= postId %>">
    <textarea cols="70" rows="8" class="reply_editor"></textarea>
    <br>
    <span class="ok_cancel">
        <input type="button" value="Submit">
        <input type="button" value="Cancel" class="cancel">
    </span>
</div>

Inline styles are the devil's handiwork.

.reply_editor {
     margin-bottom: 10px
}
.ok_cancel {
    margin-bottom: 30px;
}

For extra legibility, don't attach handlers in your HTML. Attach them using jQuery.

Comments

0
  1. Move the style stuff to a CSS file.
  2. Remove the onclick event handler, replace it with a jQuery live.
  3. Wrap the elementId in a variable.
$(".cancelButton").live("click", function(){$(this).remove();});


function Reply(postId) {
    var elementId = "reply" + postId;
    if (!document.getElementById(elementId )) {
        var element = $("#" + elementId).after("<div id='" + elementId + "'><textarea cols='70' rows='8' ></textarea><br/><span ><input type='button' value='Submit' /><input type='button' value='Cancel' class='cancelButton'/></span></div>");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.