0

I tried this one for some experiment and I can't seem to work it out can you help me about this?
The problem here is that when you click on the Radio button then click the result button it should give an alert with number(this number is how may times youve clicked the result button with the radio button on)
This is my code use:

<html>
<head>
<title>Online VOting System</title>
<script type="text/javascript">


function result()
{
if(document.test.myradio.checked==true)
}
for (i=1; i<=1; i++)
{
alert(i)
}
}

}

function load()
{
alert("hello")
}
</script>
</head>
<body bgcolor="#3ujkd2" OnLoad="load()">
<form name="test">
<input type="radio" name="myradio" />
<input type="radio" name="myradio2" />


<input type="button" onClick="result()" value="Result" />
</form>


</body>
</html>

Edited by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

2
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by kiyu2keith
1

kiyu,

Something like this maybe?

<html>
<head>
<title>Online VOting System</title>
<script type="text/javascript">
onload = function() {
	var count = 0;
	document.test.result.onclick = function() {
		count += (document.test.myradio[0].checked) ? 1 : 0;
		alert(count);
	}
}
</script>
</head>
<body bgcolor="#000099">
<form name="test">
	<input type="radio" name="myradio" />
	<input type="radio" name="myradio" />
	<input type="button" name="result" value="Result" />
</form>
</body>
</html>

Airshow

Votes + Comments
Great
0

Nicely done thanks.. I will try this. Really thanks.
Also how am I supposed to do this using text boxes, for example I clicked the first one then submit the first text box should show how many numbers I voted as well as the second box with the second button.

Edited by kiyu2keith: n/a

0

kiyu,

This is slighltly tricky, and there are many ways to write the code. It's hard for me to describe exatly what to do without actually writing it for you but here goes.

In HTML:

  • Give the radio buttons id="radio_x" , where x is 0,1,2 etc.
  • Create one input field per radio button, to display the respective vote totals. Make the input fields type="text" , size="3" , disabled (to avoid a false indication that user-input is invited), value="0" , and id="count_x" , where x is 0,1,2 etc. By giving each count field an id with the same suffix as that of its radio button, you can form a rule in Javascript to determine the id of the count field corresponding to the currently selected radio button (by taking the id of the current radio button and replacing "radio" with "count").
  • Give each voting option a label to indicate to the user what is being voted for.
  • Change the value of the "Result" button to "Vote".

In Javscript:

  • You no longer need var count . The totals will be stored in the input fields (one per voting option). There's no need to maintain totals in Javascript as well.
  • Create a var currentCountFieldId to track which radio button is currently selected. This avoids having to loop through all voting options when the "Vote" button is pressed.
  • Attach an onclick handler (ie. function) to each radio button, to set currentCountField to that radio button's id.
  • Modify the document.test.result.onclick handler to apply the rule to work out the id of the count field corresponding to currentCountFieldId (use currentCountFieldId.replace("radio", "count") ), then document.getElementById() to find the DOM element. Read its current value, convert it to a number (use Number([I]element[/I].value) ), add 1 then write it back to the same field you just read it from. With a bit of thought, much of this can be performed in one line, wrapped in a protective if(){....} to avoid an error should the user try to "Vote" before an option is selected.
  • Formulate all your javascript within onload = function() {...} to avoid using the global namespace.

Should be no more than 20 lines of javascript (I just did it in 14 including the onload wrapper and a further four lines with no more than a closing } ).

Have a hack and ask more questions if you need to.

<b>Airshow</b>

0

Can you give me an example? I can't seem to work with it

Edited by kiyu2keith: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.