Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a div dynamically by using Dojo?

I have a the following static div:

<body>
  <div id="div1"></div>
....

I want to add a div with id "div1_1" within div1 dynamically by using dojo. How can I do it?

like image 594
David.Chu.ca Avatar asked Mar 26 '09 20:03

David.Chu.ca


4 Answers

You can do it using just Dojo Base — no need to include anything, if you use the trunk or Dojo 1.3:

dojo.create("div", {id: "div1_1"}, "div1");

This line creates a div with id "div1_1" and appends it to the element with id "div1". Obviously you can add more attributes and styles in one go — read all about it in the documentation for dojo.create().

like image 68
Eugene Lazutkin Avatar answered Oct 14 '22 02:10

Eugene Lazutkin


Another option using flexible dojo.place:

dojo.place("<div id='div1_1'></div>", "div1", /*optional*/ "only");
like image 22
Kniganapolke Avatar answered Oct 14 '22 01:10

Kniganapolke


// dojo 1.7+ (AMD)
var n = domConstruct.create("div");
// dojo < 1.7
var n = dojo.create("div");
like image 20
Jeetendra Nayak Avatar answered Oct 14 '22 03:10

Jeetendra Nayak


dojo/dom-construct also can be used for creating new nodes.

A sample usage is as follows;

require([ "dojo/dom-construct", "dojo/_base/window" ], function(
        domConstruct, win) {
    // creates a new div and append it as the last child of the body
    domConstruct.create("div", null, win.body()));
});

dojo/dom-construct arguments are

  1. tag (div, h, img, li etc.)
  2. attributes ( new nodes attributes)
  3. reference node (where to place the new node)
  4. position (default last)

you can checkout the documentation for more information.

like image 39
Aksel Fatih Avatar answered Oct 14 '22 01:10

Aksel Fatih