Pregunta

I have a checkbox with a label like this:

<input type="checkbox" id="box1"/>
<label for="box1">Option 1</label>

This renders as expected ,and has the additional bonus that you can click on the label's text to check/uncheck the checkbox.

If I dynamically append more checkboxes and labels with jquery like this:

var optionsPanel = $("#optionsInnerWrapper");
    for(var i = 0; i < allOptions.length; i++)
    {
        optionsPanel.append("<div style='padding:2px;''><input id='box"+i+"' class='filterType' type='checkbox' value='"+allOptions[i]+"'><label for='box'"+i+"''>"+allOptions[i]+"</label></div>");
    }

Everything renders ok, but clicking the label will not check or uncheck the appropiate checkbox. why is this, and can i achieve this effect in another way?

¿Fue útil?

Solución 2

Your jquery code isn't right.

        optionsPanel.append("<div style='padding:2px;''><input id='box"+i+"' class='filterType' type='checkbox' value='"+allOptions[i]+"'><label for='box"+i+"'>"+allOptions[i]+"</label></div>");

What did i change?

You had too much ' here: <label for='box'"+i+"''>"+allOptions[i]+"</label>

<label for='box'"+i+"''>"+allOptions[i]+"</label>

<label for='box"+i+"'>"+allOptions[i]+"</label>

Otros consejos

"'><label for='box'"+i+"''>"

should be

"'><label for='box"+i+"'>"

There are extra single quotes in the original.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top