erikTsomik
asked on
jquery check the radio button checked
I have a set of the radio buttons when I click the radio button I have a specific logic. That is working when i make a selection. When I load a page initially it start looping through all the radio button whether it should just find the one that is check and do the logic.
$('input:radio[name="rdn"]').change(function(){
if ($("input[name='rdn']:checked").val() > 0) {
var x = $(this).attr('data-x');
if ($("#ShowFeedback").val() == 0){
$("#feedback-feedback").hide();
}else{
if ($("#ScoredFeedback").val() > 0){
alert('hello');
if (($("#ScoredFeedback").val() == 1 && x >= $("#FeedbackScore").val()) || ($("#ScoredFeedback").val() == 2 && x <= $("#FeedbackScore").val()) ){
alert('hello2');
$("#feedback-feedback").hide();
}else{
alert('hello3');
$("#feedback-feedback").show();
if ($("#leaveNote").text().length == 0){
var errMsg = "<ul>";
errMsg = errMsg+"<li>The feedback is required</li>";
errMsg=errMsg+"</ul>";
$("div#errMsg").css("background-color", "red");
$("div#errMsg").html(errMsg);
return false;
}
}
}
}
}else{
$("#feedback-feedback").hide();
}
//return false;
}).change();
ASKER
The question is when just arrive on the page why does it loop through all radio buttons when it should only executes when the one of the radio buttons is selected
$('input:radio[name="rdn"]').change(function(){
if ($("input[name='rdn']:checked").val() > 0) {
var x = $(this).attr('data-x');
if ($("#ShowFeedback").val() == 0){
$("#feedback-feedback").hide();
}else{
if ($("#ScoredFeedback").val() > 0){
alert('hello');
if (($("#ScoredFeedback").val() == 1 && x >= $("#FeedbackScore").val()) || ($("#ScoredFeedback").val() == 2 && x <= $("#FeedbackScore").val()) ){
alert('hello2');
$("#feedback-feedback").hide();
}else{
alert('hello3');
$("#feedback-feedback").show();
if ($("#leaveNote").text().length == 0){
var errMsg = "<ul>";
errMsg = errMsg+"<li>The feedback is required</li>";
errMsg=errMsg+"</ul>";
$("div#errMsg").css("background-color", "red");
$("div#errMsg").html(errMsg);
return false;
}
}
}
}
}else{
$("#feedback-feedback").hide();
}
//return false;
}).change();
Line 32 of your code above - remove
.change()
.change()
ASKER
I can not remove that line because if i come to the page that already been pre populated I want to have a trigger then too. Otherwise it will only work when I make a change not when the data is already there
Then you need a separate trigger if the page is already filled in. Something along the lines of
if($("#some_element_that_w ould_be_fi lled_in"). val()!="") {
$('input:radio[name="rdn"] ').change( )
}
You cannot have your function chained with a change() function if you want to prevent it running
if($("#some_element_that_w
$('input:radio[name="rdn"]
}
You cannot have your function chained with a change() function if you want to prevent it running
ASKER
I did this and it does not work
if ($("input[name='rdn']:chec ked").leng th > 0) {
$("input[name='rdn']:check ed").chang e();
}
if ($("input[name='rdn']:chec
$("input[name='rdn']:check
}
Try
if (!$("input[name='rdn']:che cked").val ()) {
if (!$("input[name='rdn']:che
To Cathal's point about the separate trigger, you can also fire the change after defining your method:
$(document).ready(function(){
$('input:radio[name="rdn"]').change(function(){
if ($(this).val() > 0) {
var x = $(this).attr('data-x');
if ($("#ShowFeedback").val() == 0){
$("#feedback-feedback").hide();
}else{
if ($("#ScoredFeedback").val() > 0){
alert('hello');
if (($("#ScoredFeedback").val() == 1 && x >= $("#FeedbackScore").val()) || ($("#ScoredFeedback").val() == 2 && x <= $("#FeedbackScore").val()) ){
alert('hello2');
$("#feedback-feedback").hide();
}else{
alert('hello3');
$("#feedback-feedback").show();
if ($("#leaveNote").text().length == 0){
var errMsg = "<ul>";
errMsg = errMsg+"<li>The feedback is required</li>";
errMsg=errMsg+"</ul>";
$("div#errMsg").css("background-color", "red");
$("div#errMsg").html(errMsg);
return false;
}
}
}
}
}else{
$("#feedback-feedback").hide();
}
//return false;
});
$('input:radio[name="rdn"]:checked').change();
});
He wants it firing according to a condition, not every time.
ASKER
But on page load I need the change event to fire if there already a selection, not only whne the user make a selection
Did you try
if (!$("input[name='rdn']:che cked").val ()) {
if (!$("input[name='rdn']:che
ASKER
I did but still does not work that why I had change() at the end but it loops through all radio buttons
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
p.s. use the code block tag when adding code