Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating DOM elements in jQuery

Let's say we have the following JavaScript/jQuery code below

function createElement(i, value) {     return $('<div>', { id: "field" + i, text: value}); } 

I have a div with an id of "container" and then I do:

var c=$("container"); c.append(createElement(1, "hello, world!"); 

Now I've got 2 questions

  1. Does the createElement function use jQuery to return an HTML string that gets appended to container or does it dynamically create a DOM element that gets appended to the container?

  2. I'm unfamiliar with this kind of jQuery where you actually create the HTML string (or DOM element) via the $() selector. I tried looking for the documentation on this subject in jQuery's website but I couldn't find it. Can somebody point me in the right direction?

like image 970
Jay Sun Avatar asked Aug 01 '11 18:08

Jay Sun


People also ask

What is DOM element in jQuery?

The Document Object Model (DOM) elements are something like a DIV, HTML, BODY element on the HTML page. A jQuery Selector is used to select one or more HTML elements using jQuery. Mostly we use Selectors for accessing the DOM elements.

What is the difference between jQuery elements and DOM elements?

A DOM element represents a visual or functional element on the page which was created from the original HTML document. jQuery now is a Javascript library that makes manipulating the DOM easier than with pure Javascript by offering a number of convenience shortcuts.

What are the elements of DOM?

Document object model. The DOM is the way Javascript sees its containing pages' data. It is an object that includes how the HTML/XHTML/XML is formatted, as well as the browser state. A DOM element is something like a DIV, HTML, BODY element on a page.


2 Answers

  1. It creates the DOM element on the fly, and appends that.

  2. http://api.jquery.com/jQuery/#jQuery2

like image 162
Dogbert Avatar answered Sep 16 '22 15:09

Dogbert


  1. In fact it creates the DOM element + it returns a jQuery object which contains your DOM element you just created.
  2. Dogbert was right about this, the doc is here
like image 30
MaxiWheat Avatar answered Sep 17 '22 15:09

MaxiWheat