We help IT Professionals succeed at work.

Having a problem with a confirm box thats in a radio button

mousemat24
mousemat24 asked
on
Hi there

I'm having huge problems with a confirm box in a radio button, can you please help?

If I click on a radio button where its checked I get a confirm box, this shouldnt happen, the confirm box should only appear if the radio button isnt checked.

Hope this makes sense?

I've attached the file or you can see the code below:

thanks
<html>
<head>
<script>
function ChangeAccess(territory)
{
  var orgValue = territory.value;
  var answer;
  var ukOptionButton = 

document.getElementById("ctl00_ContentPlaceHolder1_ukOptionButton");
  var globalOptionButton = 

document.getElementById("ctl00_ContentPlaceHolder1_globalOptionButton");
     
  if (territory.value == "globalOptionButton")
  {
    answer = confirm("This will clear any selections in UK Grade and UK Cost 

Centre");
  }
  else
  {
    answer = confirm("This will clear any selections in Global Grade");
  }
  

  if (answer)
  {
    document.forms[0].submit();
  }
  else
  {
    if (territory.value == "ukOptionButton")
    {
      ukOptionButton.checked = false;
      globalOptionButton.checked = true;
    } 
    else if (territory.value == "globalOptionButton")
    {
      ukOptionButton.checked = true;
      globalOptionButton.checked = false;
    }
  }
}
</script>
</head>
<body>
<form>

 <input id="ctl00_ContentPlaceHolder1_ukOptionButton" type="radio" 

name="ctl00$ContentPlaceHolder1$territoryGroup" value="ukOptionButton" 

onclick="ChangeAccess(this);" /><label 

for="ctl00_ContentPlaceHolder1_ukOptionButton">Restrict scope to UK territory 

only</label><br />
                <input id="ctl00_ContentPlaceHolder1_globalOptionButton" 

type="radio" name="ctl00$ContentPlaceHolder1$territoryGroup" 

value="globalOptionButton" checked="checked" onclick="ChangeAccess(this);" 

/><label for="ctl00_ContentPlaceHolder1_globalOptionButton">Increase scope to 

include all territories</label><br />
</form>
</body>
</html>

Open in new window

tt.htm
Comment
Watch Question

Hi, try this


function ChangeAccess(territory){
   if(territory.checked) return;
   ...
Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
Hi mousemat24,
Try 
 if (territory.checked && territory.value == "globalOptionButton") 
instead of  if (territory.value == "globalOptionButton")
Regards,
Chinmay




Author

Commented:
JohnSixkiller

If I paste you code the confirm box does not get triggered at all, so I think theres a bug

Chinmay_Patel

If I paste your code i.e.

if (territory.checked && territory.value == "globalOptionButton")
  {
    answer = confirm("This will clear any selections in UK Grade and UK Cost Centre");
  }
  else
  {
    answer = confirm("This will clear any selections in Global Grade");
  }

I still get the same error i.e. if a radio button is checked, and I click on it, I get the cofirm box.


Still not working :(
Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
Sorry but your initial question confused me a bit
Try :
if (!territory.checked && territory.value == "globalOptionButton") 
  

Author

Commented:
still not working, really sorry about this, let me explain the issue I have

I have 2 radio buttons

UK
GLOBAL

If UK is checked and I select GLOBAL I get a confirm box with a yes or no option, if I select No, it makes sure UK is Checked because we did say no, but if I click on the UK radio button I get a confirm box with the Yes or No and it will check the Global button. This happens for both the UK and GLOBAL.

i.e. if Global is checked and I select Global, I get a confirm box.

I shouldnt get a confirm box for the currently selected radio button.

Hope this makes more sense?

thnaks
Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
Yes, I think I got more clarity on the issue. But tell me you really want to do this onclick event? How about calling this script on OnChange?


Author

Commented:
I noticed there isnt a onchange event for a radio button:

http://www.hscripts.com/tutorials/javascript/dom/radio-events.php

Chinmay PatelChief Technology Ninja
Distinguished Expert 2019

Commented:
:) Trust me there is an onchange event with a radio button. Give it a shot.
Shinesh PremrajanEngineering Manager

Commented:
The things are confusing, i tried removing the checked="checked", it is replicating what you are writing, but not sure, how much closer is this.
Try following code. Just store actual selection into a global variable and reselect original radiobutton on user cancel.
<html>
<head>
<script>

var state = 'globalOptionButton';
function ChangeAccess(territory,e){
  if(state == territory.value) return;

  var text = "This will clear any selections in UK Grade and UK Cost Centre";
  if(territory.value != "globalOptionButton")  text = "This will clear any selections in Global Grade";
  
  if(confirm(text)){
	state = territory.value;
    //document.forms[0].submit();
  } else{
	 var rbs = document.getElementsByName('rbs');
	 for(var i=0; i<rbs.length; i++){
		 if(rbs[i].value == state){
			 rbs[i].checked = true;
			 return;
		 }
	 }
  }  
}
</script>
</head>
<body>
<form name='frm' id='frm'>

 <input id="ctl00_ContentPlaceHolder1_ukOptionButton" type="radio" name="rbs" value="ukOptionButton" onclick="ChangeAccess(this,event);" />
 
 <label for="ctl00_ContentPlaceHolder1_ukOptionButton">Restrict scope to UK territory only</label><br />
 
 <input id="ctl00_ContentPlaceHolder1_globalOptionButton" type="radio" name="rbs" value="globalOptionButton" checked="checked" onclick="ChangeAccess(this,event);" 

/><label for="ctl00_ContentPlaceHolder1_globalOptionButton">Increase scope to 

include all territories</label><br />
</form>
</body>
</html>

Open in new window

Author

Commented:
JohnSixkiller

Your code is almost there, I just spotted a bug, if the 1st radio button is checked i.e. ukOptionButton, and I click on it, I get a confirm box.
It is not a bug. If you change your default RB selection to any other you have to change the line

var state = 'globalOptionButton';  //<-- that string is the value of the default (checked) RB

HTH

John