Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building HTML (~= DOM) using jQuery

Tags:

html

jquery

dom

I may be doing something wrong, but I haven't been able to find a good way to build basic HTML/DOM structures, like lists, dynamically. Simple example would be building a table (table element, row, cells, with properly escaped text content) given input like json (objects).

The problem I have is that most calls (like ".append()", ".html()", ".text()") do not seem to chain in intuitive (to me, anyway) way. For example, you can't do something like:

$("#divId").append("<table>").append("<tr>").append("<td>").text("some text");

text() call seems to wipe out content at main level; and appends likewise add stuff within same scope. I would expect appennd() to return content being added, but it seems to be returning its own context.

I know there is simple "appendText()" extension that helps with last part. But how about others?

For what it's worth, right now I revert back to DOM, by something like

$("#divId")[0].appendChild(document.createElement("table"))....

but that's pretty verbose.

So I am hoping I am missing something totally obvious... but what? Call other than append()?

I tried browsing jQuery reference docs, googling, but most docs just use "embed all stuff in a string"; which has problems, including that of not quoting textual content properly.

(also: no, this is not a dup of "JQuery: Build HTML in ‘memory’ rather than DOM"

like image 627
StaxMan Avatar asked Apr 30 '09 02:04

StaxMan


People also ask

How can create DOM element in jQuery?

Answer: Use the jQuery append() or prepend() method You can add or insert elements to DOM using the jQuery append() or prepend() methods. The jQuery append() method insert content to end of matched elements, whereas the prepend() method insert content to the beginning of matched elements.

Is jQuery used for DOM manipulation?

jQuery was created in 2006 by John Resig. It was designed to handle Browser Incompatibilities and to simplify HTML DOM Manipulation, Event Handling, Animations, and Ajax.

What is DOM in jQuery with example?

The Document Object Model (DOM) is the application programming interface (API) to represent and interact with an HTML document. The DOM represents the HTML document as a tree of nodes. Every node represents a portion of the document. Explore below an example of how a simple HTML file is represented by its DOM.


2 Answers

You can do this:

$("#divId").append("<table>").append("<tr>").append("<b>").text("some text");

as either:

$("#divId").append("<table><tr><td><b>some test</b></td></tr></table>");

or

$("<b></b>").text("some text").appendTo("<td></td>").appendTo("<tr></tr>").appendTo("<table></table>").appendTo("#divId");
like image 154
cletus Avatar answered Oct 09 '22 19:10

cletus


You can use append:

$("#divId").append(
  $("<table/>").append(
    $("<tr/>").append(
      $("<td/>").append(
        $("<b/>").text("some text")
))));

If you really insist, you could use appendTo, but it's less intuitive, will be slower and takes a few more keystrokes:

$("<b/>").text("some text").appendTo(
  $("<td/>").appendTo(
    $("<tr/>").appendTo(
      $("<table/>").appendTo(
        "#divId"
))));

In any case, it's important to use .text("...") to escape special characters (and avoid XSS attacks).

Even better, when you get bored with writing these, you really want to look into jsrender, mustache, handlebars, doT or any other of the many javascript templating solutions. It allows you to keep your code separate from your HTML (think MVC) and makes it much more clear what you are building. It is much easier to modify too.

like image 24
Marc-André Lafortune Avatar answered Oct 09 '22 20:10

Marc-André Lafortune