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>

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

commented: Great +1

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.

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>

I see thanks I'll try that one

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.