Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jquery selector to add an open div tag? [duplicate]

Possible Duplicate:
Using .after() to add html closing and open tags

I am in need of adding some HTML before a <li> and after the same <li>.

The selector works, but when it adds the HTML it also closes the <div>s with adding </div>. I need it to be open.

my code:

$('#ja-megamenu .level1').before('<div class="childcontent cols1 ">');

NOTE: I need it to add ONLY <div class="childcontent cols1 "> before the li (with class="level1")

then later I'll use:

 $('#ja-megamenu .level1').after('</div>');

to add the closing </div>

This code doesn't work. How can I get this to work?

Have been reading documentation all day long, but haven't found the answer yet...

Thank you :-)

like image 399
Netthandel Avatar asked Dec 27 '22 15:12

Netthandel


2 Answers

You can not do that, if you want to add a div around an element, use jQuery's wrap().

$('#ja-megamenu .level1').wrap('<div class="childcontent cols1 "></div>');

BUT the problem you have is a div can not wrap an li! You need to do something else.

like image 94
epascarello Avatar answered Dec 29 '22 07:12

epascarello


Though this HTML is not valid (<li>'s parent could not be <div>), you may use wrap():

$("#ja-megamenu .level1").wrap("<div class='childcontent cols1' />");
like image 35
VisioN Avatar answered Dec 29 '22 06:12

VisioN