Jump to content

[SOLVED] set radio checked = true/false not working in IE


micah1701

Recommended Posts

This works in FireFox but not IE7 (haven't tested in IE6)

 

There are two radio buttons and a textfield.

If the user enters a value in the text box, it should check one radio button (with the id="choice") and uncheck the other.  If the value in the textfield is zero or blank, it checks the id="choiceNone" radio button and unchecks the other.

<script type="text/javaScript">
function checkDonation(id){
  var amount = document.getElementById('donationAmount').value;
  var zeroField = id+"None"; //var should be equal to "choiceNone"

  if (amount > 0){
document.getElementById(id).checked = true;	
        document.getElementById(zeroField).checked = false;
  }else{
document.getElementById(zeroField).checked = true;
document.getElementById(id).checked = false;	
  }
}
</script>
<input name="choice" type="radio" id="choiceNone">
<input name="choice" type="radio" id="choice" >

<input name="donationAmount" type="text" id="donationAmount" onkeyup="checkDonation('choice')" />

 

and ideas what i'm doing wrong?!!

I'm used to having CSS problems in IE but javaScript problems in one browser and not the other are new to me.  What am I missing?

Link to comment
Share on other sites

i've tested enough to make sure the script is calling the right getElementById()'s

even manually creating static vars (instead of passing values to the function).

not sure what else you might mean by "control paths"

 

for testing, I added the following line at the end of the function:

alert(id+": "+document.getElementById(id).checked+"\n"+zeroField+": "+document.getElementById(zeroField).checked)

 

When you enter a value in the textfield...

in FireFox you get

choice: true

choiceNone: false

 

but in IE you get

choice: false

choiceNone: false

 

weird?

Link to comment
Share on other sites

oh right.

yeah, i've tested for that as well, with alerts like:

  if (amount > 0){
document.getElementById(id).checked = true;	
        document.getElementById(zeroField).checked = false;
        alert("you have entered an amount greater then zero");
  }else{
document.getElementById(zeroField).checked = true;
document.getElementById(id).checked = false;	
        alert("you have entered 0 or less, or entered nothing"); 
}

 

...and i get the correct responses. so still not sure what the deal is.

Link to comment
Share on other sites

You just used a reserved word as the id 'choice'

 

change it to something else and it should work, like this

<script type="text/javaScript">
function checkDonation(id){
  var amount = document.getElementById('donationAmount').value;
  var zeroField = id+"None"; //var should be equal to "choiceNone"

  if (amount > 0){
document.getElementById(id).checked = true;	
        document.getElementById(zeroField).checked = false;
  }else{
document.getElementById(zeroField).checked = true;
document.getElementById(id).checked = false;	
  }
}
</script>
<input name="choice" type="radio" id="chsNone">
<input name="choice" type="radio" id="chs" >

<input name="donationAmount" type="text" id="donationAmount" onkeyup="checkDonation('chs')" />

Link to comment
Share on other sites

UPDATE:

You were only partially right.

 

I don't think "choice" is a reserved word - but either way, turns out that is irrelevant to my problem.

 

my problem was that one of my inputs had an ID that was the same as the NAME for both inputs.

Turns out that IE is stupid and getElementById('id') really returns what would be the getElementsByName('name') object.  go figure.

 

there quite a bit on this subject out there on google

Link to comment
Share on other sites

UPDATE:

You were only partially right.

 

I don't think "choice" is a reserved word - but either way, turns out that is irrelevant to my problem.

 

my problem was that one of my inputs had an ID that was the same as the NAME for both inputs.

Turns out that IE is stupid and getElementById('id') really returns what would be the getElementsByName('name') object.  go figure.

 

there quite a bit on this subject out there on google

No kidding... granted, I never put IDs for tags that can have NAMEs... maybe that's why.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.