javascript - create nested divs with siblings using jquery -
it's been couple of days i'm struggling this, guys give me hand.
i need create dom looks this.
<div id="nestedaccordion"> <h2>walt disney world</h2> <div> <h3>magic kingdom</h3> <div> <ol> <li>one</li> </ol> </div> <h3>epcot</h3> <div> <ol> <li>two</li> </ol> </div> <h3>hollywood studios</h3> <div> <ol> <li>three</li> </ol> </div> </div> </div>
i've tried:
$('#sidebar') .append($('<div/>').attr('id', 'nestedaccordion').html('<h2>walt disney world</h2>') .append($('<div/>').html('<h3>magic kingdom</h3>') .append($('<div/>').html('<ol><li>one</li></ol>'))) .append($('<div/>').html('<h3>epcot</h3>') .append($('<div/>').html('<ol><li>two</li></ol>'))) .append($('<div/>').html('<h3>hollywood studios</h3>') .append($('<div/>').html('<ol><li>three</li></ol>'))) )
but "walt disney world" , "magic kingdom". rest "epcot" or "hollywood studios" never shown. plus, tried several combinations of jquery 'after', 'insertafter' , 'clone()' no luck. available examples on stackoverflow demonstrate how create nested div's there no examples of nested div's sibilings. thanks!
edit: thank guys. didn't mention needed dom jquery accordion menu (not jqueryui). bowheart solution works wonderfully accordion. don't know why guest271314 solution doesn't. in case lot!
edit, updated
try
var container = $("<div>", { "id": "nestedaccordion", "html": $("<h2>", { "text": "walt disney world" }) .add( $("<div>", { "html": $("<h3>", { "text": "magic kingdom" }) .add( $("<div>", { "html": $("<ol>", { "html": $("<li>", { "text": "one" }) }) })) .add( $("<h3>", { "text": "epcot" })) .add( $("<div>", { "html": $("<ol>", { "html": $("<li>", { "text": "two" }) }) })) .add( $("<h3>", { "text": "hollywood studios" }) .add( $("<div>", { "html": $("<ol>", { "html": $("<li>", { "text": "three" }) }) }))) })) }); $("#sidebar").replacewith(container);
var container = $("<div>", { "id": "nestedaccordion", "html": $("<h2>", { "text": "walt disney world" }) .add( $("<div>", { "html": $("<h3>", { "text": "magic kingdom" }) .add( $("<div>", { "html": $("<ol>", { "html": $("<li>", { "text": "one" }) }) })) .add( $("<h3>", { "text": "epcot" })) .add( $("<div>", { "html": $("<ol>", { "html": $("<li>", { "text": "two" }) }) })) .add( $("<h3>", { "text": "hollywood studios" }) .add( $("<div>", { "html": $("<ol>", { "html": $("<li>", { "text": "three" }) }) }))) })) }); $("#sidebar").replacewith(container);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="sidebar"></div>
Comments
Post a Comment