Solved

jquery check the radio button checked

Posted on 2014-01-17
13
543 Views
Last Modified: 2014-02-05
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();

Open in new window

0
Comment
Question by:erikTsomik
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 5
13 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39789272
What is the question?

p.s. use the code block tag when adding code
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 39789285
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();

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 39789296
Line 32 of your code above - remove

.change()
0
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 
LVL 19

Author Comment

by:erikTsomik
ID: 39789603
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
0
 
LVL 58

Expert Comment

by:Gary
ID: 39789611
Then you need a separate trigger if the page is already filled in.  Something along the lines of

if($("#some_element_that_would_be_filled_in").val()!=""){
     $('input:radio[name="rdn"]').change()
}


You cannot have your function chained with a change() function if you want to prevent it running
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 39789644
I did this and it does not work

 if ($("input[name='rdn']:checked").length > 0) {
                         $("input[name='rdn']:checked").change();
                   }
0
 
LVL 58

Expert Comment

by:Gary
ID: 39789743
Try

if (!$("input[name='rdn']:checked").val()) {
0
 
LVL 12

Expert Comment

by:mkishline
ID: 39789764
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();
});

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 39789790
He wants it firing according to a condition, not every time.
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 39789828
But on page load I need the change event to fire if there already a selection, not only whne the user make a selection
0
 
LVL 58

Expert Comment

by:Gary
ID: 39789836
Did you try
if (!$("input[name='rdn']:checked").val()) {
0
 
LVL 19

Author Comment

by:erikTsomik
ID: 39789897
I did but still does not work that why I had change() at the end but it loops through all radio buttons
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39789902
Oops had it wrong

if ($("input[name='rdn']:checked").val()!="") {
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question