Anurag,

I've written a Dom element-creating plugin for jQuery.  The documentation is
here:

http://www.pinkblack.org/itblog/?page_id=22

This is how its usage looks:

var someNodes =
   $.create(
      "div", {}, [
         "span", {"class": "MyText"}, ["Hi!  How are you?"],
         "img", {"src": "Images/add.gif", "class": "AddTextButton"}, []]);

So, the basic calling pattern is this:

$.create(elementType, attributes, children, ...);  // Repeat the
elementType, attributes, children pattern to generate a bunch of elements!

$.create returns actual Dom elements; not text.

You can add the elements you generate to your document in all the expected
jQuery ways.  For example:

$("#MyPlaceholder").html(someNodes);  // Set the content.
$("#MyOtherPlaceholder").append(someNodes);  // Append to the end of this
element.

Enjoy!  And take care!
Sean


AnuragMishra wrote:
> 
> 
> Hi,
> 
> I am a jQuery beginner. I am working on an application for which I
> need to create DOM elements on the fly. Previously I was using
> document.createElement() and document.createTextNode() but that's not
> concise and looks odd along with jQuery code.
> 
> So I wrote a tiny 3 line plugin jquery.new.js. It's more of a hit and
> trial outcome.
> 
> jQuery.new = function(element) {
>       return $(document.createElement(element));
> }
> 
> I wrapped the new element that was created with the jQuery object so I
> can chain functions. For example:
> var inputBox = $.new("input").attr("type", "text").attr("id",
> "someText");
> 
> Is there a better way to do this? Or does jQuery already have this
> functionality inbuilt?
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Creating-DOM-elements-on-the-fly-tf4283829s15494.html#a12207397
Sent from the JQuery mailing list archive at Nabble.com.

Reply via email to