JQuery How to get a div in this code?
9 replies
Using an array index only returns the HTMLElement object and therefore removes a lot of useful jQuery functionality, so be careful with that. I would like to approach it like this:
var div = $("<div/>").appendTo($("<li/>").appendTo(ul));
From innermost to outermost, this creates a list element, adds it to your list, creates a div, adds it to your new list element, and returns a div still wrapped in jQuery kindness. If you like, you can add an array index to only get the HTMLElement:
var div = $("<div/>").appendTo($("<li/>").appendTo(ul))[0];
+1
source to share