2

I have an unordered list (which can nest) such as:

<div id="checklist">
    <ul>
        <li>On Page SEO
            <ul>
                <li>My work <i>And note.</i>
                <li>Your work <i>Blah</i>
            </ul>
        </li>
        <li>Is this love?</li>
    </ul>
</div>

On document ready, I want to add a checkbox to each <li> tag BEFORE the text. I currently have the following which adds it AFTER the text:

$(document).ready(function () {
     $('<input type="checkbox" value="1" />').appendTo('li');
 });
1
  • So, use prependTo() method instead of appendTo Commented Apr 1, 2014 at 3:01

3 Answers 3

3

using prependTo() instead of appendTo() will do the trick

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

Comments

0

try this:

$(document).ready(function () {
     $('div#checklist ul li').prepend('<input type="checkbox" value="1" />');
 });

Or:

$(document).ready(function () {
    $('<input type="checkbox" value="1" />').prependTo("li");
 });

Comments

0

I think what you're trying to do is to append checkbox next to any li that does not contain a ul inside it, if so then you can do:

$('ul li').not(':has("ul")').append('<input type="checkbox" value="1" />');

You also need to close the <li> using </li> in your HTML markup.

Fiddle Demo

Comments

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.