1

I want to create checkboxes dynamically. I am doing that, however I am failing with setting the name of the label. I tried setting the inner html to a value, though it didn't work. What is the correct way to do it ? source:

<!DOCTYPE html> 
<html> 
    <head> 
        <meta name="viewport" content="width=device-width, initial-scale=1"> 
        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.css" />
        <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
        <script src="http://code.jquery.com/mobile/1.2.0/jquery.mobile-1.2.0.min.js"></script>
    </head> 
    <body> 

        <div data-role="page" >
            <fieldset data-role="controlgroup" id="exampleGroup">
            </fieldset>
        </div><!-- /page -->

    </body>
    <script>
        var someId = "Something";
        for (var i = 0; i < 5; i++) {
            var checkBox = $('<input>', {
                type: "checkbox",
                class: "custom",
                id: someId + i.toString()
            });
            var checkBoxLabel = $('<label>', {
                for : someId + i.toString(),
            });
            checkBoxLabel.innerHTML = "Hello world!"; // didn't work
            checkBox.appendTo("#exampleGroup");
            checkBoxLabel.appendTo("#exampleGroup");
        }
    </script>
</html>
2
  • .innerHTML is a property of DOM elements, not jQuery elements. If you use jQuery then use jQuery methods, don't mix & match with vanilla JS. See api.jquery.com/html or api.jquery.com/text. Commented May 4, 2014 at 9:56
  • Point taken. Thank you! Commented May 4, 2014 at 9:59

3 Answers 3

1

Using text() you can set the text inside label

try this:

checkBoxLabel.text("Hello world!");
Sign up to request clarification or add additional context in comments.

Comments

0

Use text() or html() to set the contents of a DOM node. The difference between them is how they handle HTML tags.

checkBoxLabel.html('<foo>');
// <label><foo></label>

checkBoxLabel.text('<foo>');
// <label>&lt;foo&gt;</label>

You should prefer text() when you do not need HTML.

Comments

0

Try this

JSFIDDLE

var someId = "Something";
for (var i = 0; i < 5; i++) {
    var checkBox = $('<input>', {
        type: "checkbox",
        class: "custom",
        id: someId + i.toString()
    });
    var checkBoxLabel = $('<label>', {
        for: someId + i.toString(),
    });
    checkBoxLabel.text(someId + i.toString());
    checkBox.appendTo("#exampleGroup");
    checkBoxLabel.appendTo("#exampleGroup");
}

Following is the area you want to look at

checkBoxLabel.text(someId + i.toString());

I used jquery text to change the label text

http://api.jquery.com/text/

1 Comment

Done . Sorry for the mistake earlier

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.