0

I use Bootstrap and have a form with 2 textfields and 2 checkboxes. With an add-Button, I want to add (per click) an additional textfield with checkbox. Currently I'm using this JS:

$("#addButton").click(function () {

    var newTextBoxDiv = $(document.createElement('div'))
        .attr("id", 'TextBoxDiv' + counter);

    /*  TODO: patterns festlegen    */
    newTextBoxDiv.after().html('<div class="form-group label-floating">' +
        '<label class="control-label">Answer' + counter + '</label>' +
        '<input type="text" class="form-control"/>' +
        '</div><div class="togglebutton"><label><input type="checkbox" checked="">Toggle is on</input></label></div>');

    newTextBoxDiv.appendTo("#AnswerGroup");


    counter++;
});

It adds successfully the textfield, but the checkbox isn't showing up, only the text "Toggle is on".

How can I solve this? I inserted the JS inside $(document).ready(function () {.

EDIT I think the CSS causes the problem. When I simply add '</div><input type="checkbox" checked=""/></div>', it only shows the checkbox without UI. After adding the rest (like class="togglebutton", I see nothing.

7
  • I don't know if it is the problem, but the input of type checkbox do not have a value. Remove the </input> and close it directly. <input type="checkbox" checked="" /> Commented Jul 4, 2016 at 14:01
  • Post your html too Commented Jul 4, 2016 at 14:05
  • Since the JS code alone works: jsfiddle.net/9ra8vyw5 , I guess the CSS cause the problem, maybe it doesn't display checkbox inside label. Commented Jul 4, 2016 at 14:08
  • @user2181397 What do you suggest? Commented Jul 4, 2016 at 14:20
  • @Benjamin Schüller it doesnt make any difference :/ Commented Jul 4, 2016 at 14:22

5 Answers 5

1

The markup for your checkbox is not correct:

<label><input type="checkbox" checked="">Toggle is on</input></label>

should be

<label><input type="checkbox" checked="" />Toggle is on</label>
Sign up to request clarification or add additional context in comments.

1 Comment

Sadly It didnt make any difference. If I put the code directly inside my static HTML, it is working. Just when I try to add it dynamically with JS, it's not displayed.
0

May be because input tag has no closing pair.

Try

'<input type="checkbox" checked="">'

And it has to have "name" attribute

1 Comment

It doesn't make any difference
0

Tested answer

<html>
<head>
    <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script>
$( document ).ready(function() {
$("#addButton").click(function () {
    var counter = 2; //for sample
    var newTextBoxDiv = $(document.createElement('div'))
        .attr("id", 'TextBoxDiv' + counter);

    /*  TODO: patterns festlegen    */
    newTextBoxDiv.after().html('<div class="form-group label-floating">' +
        '<label class="control-label">Answer' + counter + '</label>' +
        '<input type="text" class="form-control"/>' +
        '</div><div class="togglebutton"><label><input type="checkbox" checked="">Toggle is on</input></label></div>');

    newTextBoxDiv.appendTo("#AnswerGroup");


    counter++;
});
});
    </script>
</head>
<body>
    <input type="button" id="addButton" value="hello"/>
    <div id="AnswerGroup">  </div>
</body>
</html>

3 Comments

what is the use of $(document.createElement('div'))
In this case it will create a wrapper div with ID TextBoxDiv2
If I add the label with checkbox static to html, it's working. Just when I try to add it dynamically with JS, the checkboxes are not visible (just the label).
0

First, instead of $("#addButton").click(function () { get in the habit of doing $("#addButton").on('click', function () {, you'll have more control of the event listener in the future.

Next, var newTextBoxDiv = $(document.createElement('div')).attr("id", 'TextBoxDiv' + counter); should be var newTextBoxDiv = $('<div/>').prop('id', 'TextBoxDiv' + counter);.

Next it seems you are creating content newTextBoxDiv and then inserting content after it. I'm not sure you want to do that. I would think you would want to add content to the box you just made. Additionally, according to .after(), it accepts html as an argument. I believe you calling .html() afterward is just setting the inner HTML of newTextBoxDiv anyway.

As your question specifically; you should be able to create a checkbox with the following syntax: var checkbox = $('<input/>').attr('type', 'checkbox');

To set it to checked (or unchecked) you use .prop() like this: checkbox.prop('checked', true); //checks the box.

To set it as an HTML string (and have it checked) I believe you should use the following syntax: <input type="checkbox" checked>

Comments

0

Try use:

newTextBoxDiv.append(...)

instead of

newTextBoxDiv.after().html(...)

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.