Hi I have a list of unordered elements. When clicked I would like to insert a <p>
element whilst splitting the <ul>
in 2.
The problem being that $("</ul>"+text+"<ul class=\"test\">").insertAfter(element);
seems to separate the <p>
element from the <ul>
elements, inserting the <p>
first then the <ul>
tags.
I would also like to remove the opening and closing <ul>
immediately before and after the .temp
element so I can split the <ul>
in different spot. This was the only method I could find but it only removes the <p>
element: $('.temp').remove();
The way the page is formatted at the start of this is exactly what I'm after. The JS is just bit broken. http://jsfiddle.net/yU65g/1/
JS:
$(".test li").click(function () {
var text = "<p class=\"temp\">Test</p>";
var index = $(".test li").index(this);
$("span").text("That was div index #" + index);
var element = $('.test li').get(index);
$('.temp').remove();
$("</ul>"+text+"<ul class=\"test\">").insertAfter(element);
});
HTML:
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<span>Click a div!</span>
<ul class="test">
<li>First div</li>
<li>Second div</li>
<li>Third div</li>
</ul>
<p class="temp">Test</p>
<ul class="test">
<li>Fourth div</li>
<li>Fith div</li>
<li>Sixth div</li>
</ul>
</body>
</html>
CSS:
.test li { background:yellow; margin:5px; }
span { color:red; }
.test {border: solid 1px red; clear:both; margin: 1em;}
.temp {clear:both;}
content
');
– lukeocom Feb 22 '13 at 01:33