471,892 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,892 software developers and data experts.

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 1237
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
reply views Thread by YellowAndGreen | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.