Thanks, I didn't know that you could bind an event handler like that. :)

BTW, does the "var div" now reference the new element, or does it represent
the event handler?


Jeffrey Kretz wrote:
> 
> 
> You can do it this way:
> 
> var div = $('<div></div')
>    .appendTo(document.body)
>    .attr('property',value)
>    .css({prop:val})
>    .bind('click',fn);
> 
> JK
> -----Original Message-----
> From: jquery-en@googlegroups.com [mailto:[EMAIL PROTECTED] On
> Behalf Of Yansky
> Sent: Wednesday, December 03, 2008 9:21 PM
> To: jquery-en@googlegroups.com
> Subject: [jQuery] Is there a createElement equivalent in jQuery?
> 
> 
> 
> Hi, I was just wondering if there was a createElement equivalent in
> jQuery. 
> e.g. var el = document.createElement('div');
> 
> I know I can create it with the inbuilt innerHTML way in jQuery - $('<div
> id="foo" />');
> But I like to add event handlers to the element I've just created without
> having to traverse the dom to find it and then assign an event listener.
> 
> e.g. This is how I like to do it:
> var altPlayerControlsA3 = document.createElement('a');
> altPlayerControlsA3.href='#';
> altPlayerControlsA3.setAttribute('style','margin:5px;');
> altPlayerControlsA3.id="myytplayerControlsMute";      
> altPlayerControlsA3.textContent="Mute";
> altPlayerControlsA3.addEventListener('click', function(e){
> 
>   e.preventDefault();
>   
>               //do stuff
>   
> }, false);
> 
> document.body.appendChild(altPlayerControlsA3);
> 
> but with jQuery I seem to have to do it like this:
> 
> $('body').append("&lt;a href="#" id="myytplayerControlsMute"'
> style="margin:5px;">Mute");
> $('#myytplayerControlsMute').click(function(){...
> 
> Is jQuery able to create elements not using innerHTML?
> -- 
> View this message in context:
> http://www.nabble.com/Is-there-a-createElement-equivalent-in-jQuery--tp20827
> 512s27240p20827512.html
> Sent from the jQuery General Discussion mailing list archive at
> Nabble.com.
> 
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/Is-there-a-createElement-equivalent-in-jQuery--tp20827512s27240p20828020.html
Sent from the jQuery General Discussion mailing list archive at Nabble.com.

Reply via email to