0

I have some nested lists and I want to be able to insert a previous level element:

<ul>
<li>Item 1
<ul>
<li class="selecteditem">Item 1.1</li>
<li>Item 1.2</li>
<li>Item 1.3</li>
</ul>
</li>
<li>Item 2</li>
</ul>
<!-- language: lang-js -->
//I try to do it as follows:
$('li.selecteditem').after('</ul></li><ul>"...

But the after() method only allows to insert DOM nodes, so all the closed tags "" are ignored. Is there a way to inject this html string literally exactly where I want it?

Thanks in advance for your help

1
  • I don't understand your question, can you paste how do you want your html result? Commented Nov 16, 2012 at 20:04

2 Answers 2

1

Are you looking for something like this?

$('.selecteditem').append('<li>Item 1.4</li>');​

If you use .append() you insert html to a certain element keeping the html inside it.

Example Fiddle

Sign up to request clarification or add additional context in comments.

Comments

0

I see what you need now, but DOM manipulation can only occur given DOM nodes, or valid markup that can be converted to DOM nodes. What you can do is something like this, although this is a pretty hacky solution:

var html = $('ul').html().replace('<li class="selecteditem">Item 1.1</li>','<li class="selecteditem">Item 1.1</li></ul></li><ul>...');
$('ul').html(html);

1 Comment

Yeah, been thinking about this myself too, that's pretty hacky, but if this is all we have, then it is not that hacky but the right answer...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.