Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

radio button default

Status
Not open for further replies.

Pattycake245

Programmer
Oct 31, 2003
497
CA
Excuse my lack of knowledge, but I am trying to figure out a way to do this and am not getting anywhere. I have group of radio buttons, one for each month of the year and when the page initially loads I would like it to automatically check the radio button for the current month. How can I achieve this?

thanks, Tim
 
You would probably be best off using a server side solution for this. However, since I don't know what SSL you're using (if you're even using one) you can use this client side solution to get you started.
Code:
<script language="Javascript">
var now = new Date();

function setMonth(obj) {
   obj.selectedIndex = now.getMonth();
}

</script>
<body onload="setMonth(document.getElementById('monthPulldown'))">
<select id="monthPulldown">
<option>January
<option>February
<option>March
<option>April
<option>May
<option>June
<option>July
<option>August
<option>September
<option>October
<option>November
<option>December
</select>
</body>

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Whoops, after hitting submit I noticed that you asked for radio buttons and I gave you a pulldown solution. Here's one for radio buttons (it's the same logic)
Code:
<script language="Javascript">
var now = new Date();

function setMonth(obj) {
   obj[now.getMonth()].checked = true;
}

</script>
<body onload="setMonth(document.forms['blah'].elements['monthRadio'])">
<form name="blah">
<input type="radio" name="monthRadio">January<br>
<input type="radio" name="monthRadio">February<br>
<input type="radio" name="monthRadio">March<br>
<input type="radio" name="monthRadio">April<br>
<input type="radio" name="monthRadio">May<br>
<input type="radio" name="monthRadio">June<br>
<input type="radio" name="monthRadio">July<br>
<input type="radio" name="monthRadio">August<br>
<input type="radio" name="monthRadio">September<br>
<input type="radio" name="monthRadio">October<br>
<input type="radio" name="monthRadio">November<br>
<input type="radio" name="monthRadio">December<br>
</form>
</body>

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Thanks Kaht.

I put in your code and made the necessary name changes, but when the page comes up, July, which should be checked is not? Something I'm missing?

Tim
 
Something I'm missing?
Without seeing you're code it's impossible to tell. However, I can tell you that if you copy/paste the above code I provided into a new HTML document and run it, july will be selected.

It's just a wild stab in the dark, but by any chance do you have a window.onload = doSomeJavascriptStuff declared on your page? If so, it will overwrite the onload handler in your body tag. That's just pure speculation however, if that doesn't work I'd suggest posting what you have so far.

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Sorry Kaht, I'm an idiot. I was missing this part:

obj[now.getMonth()]

thanks for your help.

Tim
 
you're welcome

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
headbang.gif
[rockband]
headbang.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top