469,331 Members | 5,302 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,331 developers. It's quick & easy.

How to make a Radio ButtonValue = A javascript variable

Hi I have what seems like a simple question but it has baffled me for hours now. How do I set the value of an HTML radio button to a predefined javascript value? I am trying to get the value of radioButton to = the javascript variable named countryText which is defined earlier in the page.

The following does not work obviously:

<input name="radioButton" type="radio" class="box" value="javascript:countryText" checked="checked" "/>

Any advice? thanks.
Mar 29 '07 #1
1 1213
iam_clint
1,208 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. <body onload="setvalue();">
  2. <script>
  3. function setvalue() {
  4. document.getElementById("radioButton").value = country
  5. }
  6. </script>
  7. <input name="radioButton" id="radioButton" type="radio" class="box" value="" checked="checked" "/>
  8.  
You need to make sure that country is a global variable... meaning setting it out of a function EX.
This would work
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. var country = '';
  3. function blah() {
  4. country = "USA";
  5. }
  6. </script>
  7.  
This wouldn't work
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function blah() {
  3. country = "USA";
  4. }
  5. </script>
  6.  
because it was set inside the function only so when another function tries to call that variable it will return nothing. But since in the first code it was first declared outside of the function it can be used again on the page outside of that function.
Mar 29 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

23 posts views Thread by Phil Powell | last post: by
10 posts views Thread by DettCom | last post: by
4 posts views Thread by Oscar Monteiro | last post: by
1 post views Thread by MickG | last post: by
2 posts views Thread by Mark Haase | last post: by
2 posts views Thread by Karl | last post: by
6 posts views Thread by scottyman | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Purva khokhar | last post: by
1 post views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.