0

I have this checkbox created using forms.py. I did not write html code, it's resulted rendering forms.py.

<div id="div_id_diag-diagnosis_option" class="form-group">
<label for="id_diag-diagnosis_option_0" class="control-label col-md-3 requiredField">
Option<span class="asteriskField">*</span></label>
<div class="controls col-md-8"><label class="checkbox">
<input type="checkbox" name="diag-diagnosis_option" id="id_diag-diagnosis_option_1" value="b" >b</label>
<label class="checkbox">
<input type="checkbox" name="diag-diagnosis_option" id="id_diag-diagnosis_option_2" value="a" >a</label>
<label class="checkbox">
<input type="checkbox" name="diag-diagnosis_option" id="id_diag-diagnosis_option_3" value="c" >c</label>
</div></div>

Using javascript I want to create an extra checkbox near to every option. For example if a user selects b option, on the right of b I want a new checkbox to be added.

Any ideas please?

4
  • use jquery onclick event and append a checkbox... Commented Oct 7, 2015 at 9:54
  • @ShiguriAnemone' Can you give me a simple example please? Commented Oct 7, 2015 at 9:57
  • Do you want this to happen indefinitely? (eg. A clicked = B appears; B clicked = C appears; C clicked = D appears and so on) Or only once? Commented Oct 7, 2015 at 10:05
  • @Fester If a clicked, a new checkbox is created and appeared next to a. A,B,C should be appeared from the beginning Commented Oct 7, 2015 at 10:08

3 Answers 3

1

this adds a checkbox next to the checkbox you selected

$("input[type='checkbox']").click(function(){
   $(this).after('<input type="checkbox">');
})

Demo

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

3 Comments

Insert this line $(this).after('<input type="checkbox">'); before the alert and it should work @zinon
it will add the checkbox, but its not visible, inspect with chrome or firebug
I think it is because there is no label.
0

Try something like this

$("input[type='checkbox']").click(function(){
    if ($(this).prop('checked'))
    $(this).after('<input type="checkbox">');
});

Comments

0

try something like this

$("input[type='checkbox']").click(function(){
     $(this).after('<input type="checkbox" name="diag-diagnosis_option" id="id_diag-diagnosis_option_2" value="a" >');
})

JSFIDDLE CODE SAMPLE

1 Comment

Thank You. I do not to have a button and only next to the selected option (or optionS) a new checkbox will be appeared not to all.

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.