Monday 10 March 2014

jquery create elements on the fly

$("<div/>",{
    "class" : "someelement",
    // .. you can go on and add properties
    "css" : {
        "color" : "red"
    },
    "click" : function(){
        alert("you just clicked me!!");
    },
    "data" : {
       "foo" : "bar"
    }
}).appendTo("#container");
create div and set id ,class,css ,event on the fly
This is the way to go if youre setting multiple properties. Much cleaner than passing in a long string.
I would like to have feedback from my blog readers. 
Your valuable feedback, question, or comments about this article 
are always welcome.

No comments:

Post a Comment