1 Replies - 2863 Views - Last Post: 23 July 2012 - 01:09 PM

#1 ihateschoolprojects  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 02-February 12

Radio button value alert and clicks counter

Posted 23 July 2012 - 12:41 PM

This is an activity to be given by our teacher and now I'm trying to figure out how to.
How can I create JS and HTML codes that would alert the value of a radio button (that was clicked) and at the same time,
the number on how many times you'd clicked it.

I've searched and the internet and found out two codes that 'may' help me.

1st - alerting radio buttons

<html>
<head>
<script type="text/javascript">
	function GetSelectedItem()
	{
	
	var chosen=""
	var i;
	var len = document.f1.r1.length;
	
	for (i=0;i<len;i++)
		{
			if (document.f1.r1[i].checked)
			{
				chosen = document.f1.r1[i].value
			}	
		}
		
	if (chosen == "")
		{
			 alert("No chosen radio");			 
		}
	
	else
		{
			alert(chosen)
		}
	}
	
</script>	

</head>
<body>

<h1>This test will count how many times you clicked each button</h1>
	<form name="f1">
		<input type="radio" name="r1" value="You clicked A">A
		<input type="radio" name="r1" value="You clicked B">B
		<input type="radio" name="r1" value="You clicked C">C
		<input type="radio" name="r1" value="You clicked D">D
	</form>
		<button type="button" name="akosibuton" onclick="GetSelectedItem()"/>Count kung pila
</html>






2nd - clicks counter
<!--  
   The tutorial explains the simple Javascript function of counting the  
   number of times the button has been clicked.  
-->  
  
<html>  
<head>  
<title> Count the Number of Button Clicks </title>  
  
<script language="Javascript" type="text/javascript">  
var noOfClicks = 0;      // Used to count the number of Clicks.  
function button_click()  
{  
   noOfClicks++;   // On click of the button the value is incremented.  
  
   //   The below line changes the text written on the button to display the number  
   //   of times the button has been clicked.  
   window.document.ButtonForm.myButton.value="Clicked " + noOfClicks + " times";  
}  
</script>  
  
<body>  
<form name="ButtonForm">  
<p> Click the Button Below </p>  
  
<!--  
   A simple page with just a button is displayed and on the click of the button  
   the text on the button changes to let the user know how many times the button has  
   been clicked.  
-->  
<input type="button" name= "myButton" value="Click Me" onclick="button_click()">  
</form>  
  
</body>  
  
</head>  
</html>  






So I've been thinking that If I'll be able to combine the two codes, I just might get the answer to my problem. Now my question is, how to?

Is This A Good Question/Topic? 0
  • +

Replies To: Radio button value alert and clicks counter

#2 BenignDesign  Icon User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 6216
  • View blog
  • Posts: 10,731
  • Joined: 28-September 07

Re: Radio button value alert and clicks counter

Posted 23 July 2012 - 01:09 PM

Crazy idea here, but perhaps you would both learn more AND understand more if you were to write your own code for this assignment versus copy-pasting someone else's work and asking us to mash it together for you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1