I have used the following code with radio button lists (generally 2 choices). This has allowed me to place another input (textbox, another radio button list, etc.) inside a <div> that would be hidden until a button was clicked.
<script type="text/javascript">
$(document).ready(function () {
$("input[name$='RadioButton1']").on ( "change", function() {
if (this.value == "Yes") {
$('#someDisplay, #someDisplay input').show().removeAttr("disabled");
}
else {
$('#someDisplay, #someDisplay input').hide().attr("disabled", "disabled");
}
});
$("#someDisplay, #someDisplay input").hide().attr("disabled", "disabled");
});
</script>
Again, the great thing about this is that the input inside the <div> would remain disabled if nothing was clicked, which meant that no information was passed when the form was submitted (i.e., the input was "empty" or "null" and nothing had to be saved).
Now I am trying to use this code to work with a checkbox list. Unfortunately, if you clicked all the checkboxes it would only show the last clicked checkbox <div> even though prior checkboxes were clicked. I tried using JQuery toggle(), and while it would show/hide the <div> corresponding to what was clicked, the problem is if you un-clicked a checkbox, the input that was inside the <div> would remain enabled. Sorry if I am not clear. Here is what I tried:
<script type="text/javascript">
$(document).ready(function () {
$("input[name$='Checkbox1']").on ( "change", function() {
if (this.value == "Choice 1") {
$('#someDisplay1, #someDisplay1 input').show().removeAttr("disabled");
}
else {
$('#someDisplay1, #someDisplay1 input').hide().attr("disabled", "disabled");
}
if (this.value == "Choice 2") {
$('#someDisplay2, #someDisplay2 input').show().removeAttr("disabled");
}
else {
$('#someDisplay2, #someDisplay2 input').hide().attr("disabled", "disabled");
}
});
$("#someDisplay1, #someDisplay1 input").hide().attr("disabled", "disabled");
$("#someDisplay2, #someDisplay2 input").hide().attr("disabled", "disabled");
});
</script>
I was thinking that it might have been the logic of the if/else.
And this is the checkbox input html:
<ol>
<li>
<label>
<input name="Checkbox1" type="checkbox" value="Choice 1" data-val="true">
Choice 1
</label>
</li>
<li>
<label>
<input name="Checkbox1" type="checkbox" value="Choice 2">
Choice 2
</label>
</li>
</ol>
Thank you in advance for any help.
EDIT: To add the hidden <div>:
EDIT2: Accidently deleted disabled="disabled"
<div disabled="disabled" id="someDisplay1" style="display: none;">
<div class="form-group">
<label>Display 1</label>
<input name="TextBox1" style="display: none;" type="text" value="" />
</div>
</div>
<div>. Note that when a click occurs thedisplay:turns toblockin the first<div>and for the<input>it becomesinline-block.toggle()instead, but what happens is that all the inputs corresponding to the checkbox originally clicked - but then later un-clicked - would remain enabled and submit a "false" or "default" value, which is not what I want. If it is not checked I don't want any value submitted for the corresponding<div>that is hidden.