$(document).ready(function() {
var childrenEle;
function toggleReq() {
$('fieldset.fieldsetClass').each(function() {
id = $(this).attr('id');
$("#" + id).children().not("div,legend").each(function () {
var divEle = $("div").attr('id');
if($(this).attr('class') == 'required') {
$(this).css("display","block");
}
else {
$(this).css("display","none");
}
});
});
}
function toggleAll() {
$('fieldset.fieldsetClass').each(function() {
id = $(this).attr('id');
$("#" + id).children().each(function () { $(this).css("display","block");
});
});
}
$("div").toggle(
function toggle() {
$('fieldset.fieldsetClass').each(function() {
id = $(this).attr('id');
$("#" + id).children().each(function () { $(this).css("display","block");
});
});
},
function() {
$('fieldset.fieldsetClass').each(function() {
id = $(this).attr('id');
$("#" + id).children().not("div,legend").each(function () {
if($(this).attr('class') == 'required') {
$(this).css("display","block");
}
else {
$(this).css("display","none");
}
});
});
}
);
/*$("div").toggle(function(){
toggleAll()
},function(){
toggleReq()
}); */
$.onload = toggleReq();
});
</script>
</head>
<body>
<form name="formID" id="questions-form" class="formular" method="post" action="">
<fieldset class="fieldsetClass" id="question-0">
<legend>Question 1</legend><div id="toggle1">Click Here</div>
<input class="required" type="radio" name="answer[0]" value="0" id="radio-0-0" /><label class="required" for="radio-0-0">Green</label>
<input class="required" type="radio" name="answer[0]" value="1" id="radio-0-1" /><label class="required" for="radio-0-1">Red</label>
<input type="radio" name="answer[0]" value="2" id="radio-0-2" /><label for="radio-0-2">Orange</label>
</fieldset>
<fieldset class="fieldsetClass" id="question-1"><div id="toggle2">Click Here</div>
<legend>Question 2</legend>
<input type="radio" name="answer[1]" value="0" id="radio-1-0" /><label for="radio-1-0">21</label>
<input class="required" type="radio" name="answer[1]" value="1" id="radio-1-1" /><label class="required" for="radio-1-1">11</label>
<input class="required" type="radio" name="answer[1]" value="2" id="radio-1-2" /><label class="required" for="radio-1-2">23</label>
</fieldset>
</form>
Hi Folks, From the pasted above code I am trying to loop through every fieldset element with jquery each function, and again iam using one more jquery each function for looping through the every child element - this is the common idea for looping both the functions - toggleReq(), toggleAll().
In toggleReq() through looping Iam segregating the child elements which has got CLASS ATTRIBUTE value as "REQUIRED" and displaying them and hiding rest of the child elements.
So during page onload iam displaying only the child elements which have got CLASS ATTRIBUTE value as "REQUIRED" thru jquery onload.
In toggleAll() iam looping through all child elements and displaying all the required and optional child elements even.
I hope now you guys got the explanations for both the functions
But there is one trap here, the thing is like there should be one clickable icon for every fieldset, so when the user clicks the
icon it should display all the child elements and when click again it should display only the REQUIRED fields, basically like toggling :) and this toggling should happen only for one fieldset element at a time.
In my code this toggling happens simultaneously for all the fieldset elements and this is not the correct one.
Please any one help me out how to make this toggling function should work independent for the fieldset elements.
please help me out with some ideas for this..
Thanks
jaykom