By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,521 Members | 1,274 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,521 IT Pros & Developers. It's quick & easy.

Dynamically change text box based on radio button

P: 2
I have the following. Based on which radio button is checked, I want to adjust the date that is filled in for the text box. The problem I am having is that each button fills in the same date. Am I misunderstanding what getElementById("opt1").value returns? TIA

[HTML]<html>
<head>
<script language="JavaScript">

function changeRadioButton(iDays)
{
if (document.getElementById("opt1").value == 'week')
{iDays = 7}
else
{iDays = 14 }

var myDate=new Date()
myDate.setDate(myDate.getDate()+ iDays)

document.getElementById("dateBox").value = myDate;
}
</script>
</head>
<body>
<input onclick="changeRadioButton()" type="radio" name="priority" id="opt1" >week</input>
<input onclick="changeRadioButton()" type="radio" name="priority" id="opt2" >month</input>
<input onclick="changeRadioButton()" type="radio" name="priority" id="opt3" >year</input>
<input type="text" id="dateBox" value=""/>
</body>
</html>[/HTML]
Nov 12 '07 #1
Share this Question
Share on Google+
1 Reply


P: 9
Hi...
There's some code may be help you on the blow.
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script language="JavaScript">
  4.  
  5. function changeRadioButton(el){
  6.     var iDays;
  7.     if (el.value == 'week'){
  8.         iDays = 7
  9.     }else if(el.value == 'month'){
  10.         iDays = 14
  11.     }else{
  12.         iDays = 0;
  13.     }
  14.  
  15.     var myDate=new Date()
  16.     myDate.setDate(myDate.getDate()+ iDays)
  17.     document.getElementById("dateBox").value = myDate;
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <input onclick="changeRadioButton(this)" type="radio" value="week">week</input>
  23. <input onclick="changeRadioButton(this)" type="radio" value="month">month</input>
  24. <input onclick="changeRadioButton(this)" type="radio" value="year">year</input>
  25. <input type="text" id="dateBox" value=""/>
  26. </body>
  27. </html>
  28.  
You have't use this keyword and assign the value of input elements.
Nov 13 '07 #2

Post your reply

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