944,209 Members | Top Members by Rank

Ad:
Mar 20th, 2006
0

javascript radio buttons and insanity

Expand Post »
I am (as you will quickly discover) a novice javascript coder. Here is my issue. I am using a radio button, and it works fine. Then when I change some other element on the page, or hit refresh, the radio button does not remain set. You can see it in action here:

http://claytargetsonline.com/radio2.php?type=line

I have tried check to see which one is checked and every other solution I can think of. I have asked experienced programmers, and didn't want to waste more than 10 minutes of their time, but it was not obvious to them either.

I have spent about 5 hours searching the web. No avail. Help!

I did search this forum also but did not find a similar problem.

Any help woule be appreciated.

David

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type="text/javascript">
  2.  
  3. function check(charttype)
  4. {
  5. document.getElementById("chartType").value=charttype
  6. }
  7.  
  8. function refresh_window()
  9. {
  10.  
  11. var type = document.getElementById("chartType").value
  12. window.location=("radio2.php?type=" + type)
  13.  
  14. }
  15.  
  16.  
  17. </script>
  18.  
  19.  
  20. <form id="form1" action="(EmptyReference!)" method="put" name="form1">
  21.  
  22. <table width="415" border="0" cellspacing="2" cellpadding="0">
  23. <tr>
  24. <td width="297"><strong>Chart Type:</strong></td>
  25. <td valign="bottom" width="112">&nbsp;</td>
  26. </tr>
  27. <tr>
  28. <td width="297">
  29. <input type="radio" name="chartType" id="chartType" value="line" onclick="check(this.value)" <?php if ($type == "line") echo "CHECKED" ?> > line &nbsp;
  30. <input type="radio" name="chartType" id="chartType" value="column" onclick="check(this.value)" <?php if ($type == "column") echo "CHECKED"?> > column <input type="radio" name="chartType" id="chartType" value="3dcolumn" onclick="check(this.value)" <?php if ($type == "3dcolumn") echo "CHECKED"?> > 3d column
  31. </td>
  32. <td align="right" rowspan="2" valign="bottom" width="112">
  33. <button name="refresh" type="button" onclick="refresh_window()">Refresh Chart </button>
  34. </td>
  35. </tr>
  36. <tr>
  37. <td width="297"></td>
  38. </tr>
  39. </table>
  40.  
  41. </form>
  42. <p>The problems is this. </p>
  43. <p>Set the chart type to column and hit refresh. Everything works.</p>
  44. <p>Now hit refresh again. Chart type changes to line.</p>
  45. <p>I hope someone can help me with this. I am losing my mind.</p>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
davidpitts is offline Offline
5 posts
since Mar 2006
Mar 20th, 2006
0

Re: javascript radio buttons and insanity

Not sure I understand the problem. Hitting "Refresh" will, well, refresh the page. No value would be expected to remain, every element would naturally go back to the initial, default values.
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Mar 20th, 2006
0

Re: javascript radio buttons and insanity

The answer is simple. You have the onclick events launched when you click a radio button. That function (check) sets a document item to the value of the clicked option. Then when you execute a refresh_window, that value is still valid in that page element, when you read the value in order to pass it to window.location, which is good, but when the page refreshes, so does the page element that normally gets set by the check function. So, since the check function isn't executed again, it resorts to it's default value. You can illustrate this, by going to the page, choosing an option, and click refresh, then click the same option, and click refresh again..... as long as you click an option, it remains checked on the refresh... it's only when you do a refresh, and then another refresh (because you aren't calling the check function on the second refresh) that it loses it's value. Now, I'm no programmer, but I'd suggest trying to add a call to the check function inside the refresh_window function....


As a very last resort, assuming the above doesn't solve the issue.... you could have the page write a cookie, and then read the cookie for the value that was previously set.... a tacky fix, but one that would probably work.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Mar 20th, 2006
0

Re: javascript radio buttons and insanity

I tried to simplify the code so that it would be the simplest for asking the question. Maybe the full example of what I am trying to do would be better.

I understand what you are saying about refresh setting the state back to the default, but checkboxes stay checked, why do radio buttons reset?

http://claytargetsonline.com/napl/ch...alse&type=line

This link will take you to the charting program that I am working on. If you select a column chart and hit refresh, everything works great. Then if you change ONLY the number of Months and refresh again, it resets the chart type to line.

Any suggestions for how to preserve the setting for columns? Neither the drop down nor the checkboxes seem to have the problem.

Thanks for the responses so far.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
davidpitts is offline Offline
5 posts
since Mar 2006
Mar 20th, 2006
0

Re: javascript radio buttons and insanity

Yeah, I said cookies....

But beyond, the code that you have in both what you posted, and the site in the link..... when you click the radio button, it executes an onclick event.... right? That function is called "check", and it gets called when you click an option button. The check box's don't call any kind of function at all, but the option buttons do. Why the checkbox's retain their value, and the option buttons do not, is beyond me.... but I'll bet if you call the check function within the refresh_window function, it should work just fine.... Look carefully at this line:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <input type="radio" name="chartType" id="chartType" value="column" onclick="check(this.value)" > column
onclick, it calls check(this.value), when you hit refresh, the first time, it works if you change the option button, because you clicked the option button, and it called this function... when you click it the second time, it never calls "check".... ever. Not once... it needs to. So, somewhere in the refresh_window function, you need to have it call the check function.

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function refresh_window()
  2. {
  3. var type = document.getElementById("chartType").value
  4. check(type);
  5. window.location=("radio2.php?type=" + type)
  6. }

Or something along those lines...
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Mar 20th, 2006
0

Re: javascript radio buttons and insanity

Thanks. I will give it a try.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
davidpitts is offline Offline
5 posts
since Mar 2006
Mar 22nd, 2006
0

Re: javascript radio buttons and insanity

OK. I tried, but I never got it to work. Thanks for the help, but now a lovely dropdown menu that does work sits in place of the radio buttons. I am a beaten man. :-|
Reputation Points: 10
Solved Threads: 0
Newbie Poster
davidpitts is offline Offline
5 posts
since Mar 2006
Mar 22nd, 2006
0

Re: javascript radio buttons and insanity

Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Mar 22nd, 2006
0

Re: javascript radio buttons and insanity

why not use cookies?
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Mar 25th, 2006
0

Re: javascript radio buttons and insanity

Quote originally posted by Comatose ...
why not use cookies?
Remember the part at the start of the thread about being inexperienced? Well cookies is still something that I have not done. :o Soon though. I just can't figure out why the radio buttons lose their value and dropdowns and checkboxes don't. Go figure.

Thanks for your help. It is much appreciated.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
davidpitts is offline Offline
5 posts
since Mar 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Calling Different Iframes in Page
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: Please help, been a few years.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC