2

I want to create multiple checkboxes with JavaScript. I tried the following:

var response= data;
        var container = document.createElement('div');
        var checkboxContainer = document.createElement('div');
        checkboxContainer.setAttribute('class', 'checkbox');
        var label;
        var checkBox;

        for(i=0;i<response.length;i++){
            label = document.createElement('label');
            checkBox = document.createElement('input');
            checkBox.type = "checkbox";
            checkBox.name = "selDates[]";
            checkBox.value = response[i]['date'];

            label.innerHTML = response[i]['date'] + " " + response[i]['typ'];
            label.appendChild(checkBox);

            checkboxContainer.appendChild(label);
            container.appendChild(checkboxContainer);
        }
        $("#downloadContent").prepend(container);

As I can see in the chrome developer tools it creates all my checkboxes, but only display one. All labels are displayed.

enter image description here

As you can see, one checkbox is displayed with all (3) labels.

enter image description here

Here you can see a print screen of the code.

Why is there just one checkbox displayed? For your information, I use Bootstrap 3 and jQuery.

Thanks for your help!

Yanick

5
  • 1
    can you provide a fiddle...? and post the content inside checkbox css? Commented Apr 7, 2014 at 19:08
  • does your class checkbox or any other css style do anything to hide the other checkboxes? Commented Apr 7, 2014 at 19:11
  • Have you seen that the checkbox is shown before the text and should be shown after it? You probably have a CSS issue. Commented Apr 7, 2014 at 19:15
  • Well, to test this I've set the Bootstrap label class "checkbox-inline" but I get the same result. I am currently working on a fiddle for you guys.. @Moloo Salsas: Yes I've seen this. It's correct Commented Apr 7, 2014 at 19:15
  • As I've said, if it is correct, you probably have a CSS issue. Try disabling CSS to see if it works right. Commented Apr 7, 2014 at 19:20

1 Answer 1

1

The Javascript seems ok (except may be for adding checkboxContainer every time in the loop).

One possibility is that CSS makes the other checkboxes not visible.

For example the style for those labels could be defined with position:absolute (thus all of them are visible, but one above the other), or display:none except for one...

Hard to tell for sure without a minimal reproducible example.

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

5 Comments

This is not an answer at all. Should be a comment.
@ManoloSalsas: I think it's an answer (CSS is the reason the other checkboxes are not visible). Of course without a reproducible example I cannot be 100% sure.
well if I set display:none to my labels nothing is displayed, because the checkboxes are in the label tag...
@Yanick: CSS can select specific elements (for example :first-child)
You were right, after add add a <br> after every label, the are now displayed... thx allot. Is there any "nicer" way than a <br> after every label to solve this?

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.