OnClick event handling of a period button in JavaScript form
On click event of a period button can be used to process different actions. This event can be connected to submission of a form or can be connected to redirecting to different pages or the page it self can be reloaded. Here is a simple example of on click event of a period button where the page is reloaded with the available value of the clicked period button. We will not discuss how to use the value available after the page reloads and you can get an idea in our PHP section on this. Here we will discuss how to reload the page with the value of the checked period button.
Here is our two period buttons with OnClick event calling a function in JavaScript.
You can see the onclick event is calling function reload(). Inside the function reload we will have some object properties to identify the different objects. Here is one to identify the number of period buttons used inside the form.
document.form1.type.length
Here form1 is the name of the form and type is the name of the period button. From this length value we will loop through one for loop to check each element status ( checked or not ). We can get the status of the period button by checking the value of
document.form1.type[i].checked
Which returns true or false if the button is clicked or not. The value of i changes inside the for loop. If the period button is checked then we will get the value of that button and we will store that value in a variable declared as val.
Outside the loop we will redirect the page with the value of the period button clicked using a query string.
self.location='dd.php?cat=' + val ;
Here is the demo of the code. You can select and see the query string in your address bar with the value of the period button.
Here is the code of the JavaScript function and below that the period button.
<html>
<head>
<title></title>
<script type="text/javascript">
function reload()
{
for(var i=0; i < document.form1.type.length; i++){
if(document.form1.type[i].checked)
var val=document.form1.type[i].value
}