Reputation: 1398
The form has one or more groups of radio buttons on it. It is possible for the value to start off with the value of ^. Once another value is selected, ^ is no longer viable, so it's div needs to be hidden. Here is the source and the html, along with a link to it in jsfiddle: http://jsfiddle.net/RSNxS/
$(function(){
$('.tristateRadio').bind("change", handleTristateRadioChange);
function handleTristateRadioChange(e) {
var button = $this;
var id = button.id();
$("#"+id).filter(
function(){ this.value == "^"}
).parent().hide();
}
});
html
$<div class="questionItem">
<h3>C0900A</h3>
Staff asmt mental status: recall current season <br/>
<div class="questionCheckbox"><input class="tristateRadio" id="answers_12__Answer" name="answers[12].Answer" type="radio" value="0" /> (0) Not checked (No)</div>
<div class="questionCheckbox"><input class="tristateRadio" id="answers_12__Answer" name="answers[12].Answer" type="radio" value="1" /> (1) Checked (Yes)</div>
<div class="questionCheckbox"><input class="tristateRadio" id="answers_12__Answer" name="answers[12].Answer" type="radio" value="-" /> (-) Not assessed</div>
<div class="questionCheckbox"><input checked="checked" class="tristateRadio" id="answers_12__Answer" name="answers[12].Answer" type="radio" value="^" /> (^) Blank (skip pattern)</div>
</div>
<div class="questionItem">
<h3>C0900B</h3>
Staff asmt mental status: recall location of room <br/>
<div class="questionCheckbox"><input class="tristateRadio" id="answers_13__Answer" name="answers[13].Answer" type="radio" value="0" /> (0) Not checked (No)</div>
<div class="questionCheckbox"><input class="tristateRadio" id="answers_13__Answer" name="answers[13].Answer" type="radio" value="1" /> (1) Checked (Yes)</div>
<div class="questionCheckbox"><input class="tristateRadio" id="answers_13__Answer" name="answers[13].Answer" type="radio" value="-" /> (-) Not assessed</div>
<div class="questionCheckbox"><input checked="checked" class="tristateRadio" id="answers_13__Answer" name="answers[13].Answer" type="radio" value="^" /> (^) Blank (skip pattern)</div>
</div>
<div class="questionItem">
<h3>C0900C</h3>
Staff asmt mental status: recall staff names/faces <br/>
<div class="questionCheckbox"><input class="tristateRadio" id="answers_14__Answer" name="answers[14].Answer" type="radio" value="0" /> (0) Not checked (No)</div>
<div class="questionCheckbox"><input class="tristateRadio" id="answers_14__Answer" name="answers[14].Answer" type="radio" value="1" /> (1) Checked (Yes)</div>
<div class="questionCheckbox"><input class="tristateRadio" id="answers_14__Answer" name="answers[14].Answer" type="radio" value="-" /> (-) Not assessed</div>
<div class="questionCheckbox"><input checked="checked" class="tristateRadio" id="answers_14__Answer" name="answers[14].Answer" type="radio" value="^" /> (^) Blank (skip pattern)</div>
</div>
<div class="questionItem">
<h3>C0900D</h3>
Staff asmt mental status: recall in nursing home <br/>
<div class="questionCheckbox"><input class="tristateRadio" id="answers_15__Answer" name="answers[15].Answer" type="radio" value="0" /> (0) Not checked (No)</div>
<div class="questionCheckbox"><input class="tristateRadio" id="answers_15__Answer" name="answers[15].Answer" type="radio" value="1" /> (1) Checked (Yes)</div>
<div class="questionCheckbox"><input class="tristateRadio" id="answers_15__Answer" name="answers[15].Answer" type="radio" value="-" /> (-) Not assessed</div>
<div class="questionCheckbox"><input checked="checked" class="tristateRadio" id="answers_15__Answer" name="answers[15].Answer" type="radio" value="^" /> (^) Blank (skip pattern)</div>
</div>
<div class="questionItem">
<h3>C0900Z</h3>
Staff asmt mental status: none of above recalled <br/>
<div class="questionCheckbox"><input class="tristateRadio" id="answers_16__Answer" name="answers[16].Answer" type="radio" value="0" /> (0) Not checked (No)</div>
<div class="questionCheckbox"><input class="tristateRadio" id="answers_16__Answer" name="answers[16].Answer" type="radio" value="1" /> (1) Checked (Yes)</div>
<div class="questionCheckbox"><input class="tristateRadio" id="answers_16__Answer" name="answers[16].Answer" type="radio" value="-" /> (-) Not assessed</div>
<div class="questionCheckbox"><input checked="checked" class="tristateRadio" id="answers_16__Answer" name="answers[16].Answer" type="radio" value="^" /> (^) Blank (skip pattern)</div>
</div>
Upvotes: 0
Views: 380
Reputation: 57958
your code is all kinds of broken:
$this
variable .id()
function this
$
for readabilityedit: my code sucks, i leave you just the comments
Upvotes: 2
Reputation: 43823
You should definitely fix the multiple id issues, which is invalid HTML and could cause undesired and unexpected behaviour (as others have already mentioned).
However the following does not use selection by id and will hide the last <input>
with value = ^
in a group, when any of the other siblings are changed:
$('.tristateRadio').bind('change', function() {
$(this).parent().siblings(':last').hide();
});
The original code in the question is not working because $this
is undefined. I think you probably meant $(this)
in order to wrap the native object in jQuery. These sorts of errors are usually shown in the browser's console or in an dialog and are the first place to look when debugging JavaScript.
Upvotes: 1
Reputation: 16961
It doesn't work because there can only be ONE id
on a page. jQuery assumes this, and will only select one of your radio buttons, therefore never finding any with the value '^'.
Simply change it to a class and it will work (or use some other kind of identification - like name
)
$("."+theClass).filter(
function(){ this.value == "^"}
).parent().hide();
You may also choose to use CSS selectors:
$("."+theClass+"[value='^']").parent().hide();
Upvotes: 1