Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML button onclick event

3 buttons 3 onclick events

This might be a very basic question; believe me I found very hard to find the answer to this question on the internet. I have 3 HTML pages stored in my server (Tomcat locally) & i want to open these HTML pages on a button click. Any help would be highly appreciated.

Here is my code,

<!DOCTYPE html> <html>   <head>       <meta charset="ISO-8859-1">       <title>Online Student Portal</title>   </head> <body>   <form action="">       <h2>Select Your Choice..</h2>       <input type="button" value="Add Students">       <input type="button" value="Add Courses">       <input type="button" value="Student Payments">   </form> </body> </html> 
like image 637
codezoner Avatar asked Sep 08 '13 03:09

codezoner


2 Answers

Try this:-

<!DOCTYPE html>  <html>  <head>      <meta charset="ISO-8859-1">      <title>Online Student Portal</title>  </head>  <body>      <form action="">          <input type="button" value="Add Students" onclick="window.location.href='Students.html';"/>          <input type="button" value="Add Courses" onclick="window.location.href='Courses.html';"/>          <input type="button" value="Student Payments" onclick="window.location.href='Payment.html';"/>     </form>  </body>  </html> 
like image 136
Rahul Tripathi Avatar answered Sep 23 '22 01:09

Rahul Tripathi


You should all know this is inline scripting and is not a good practice at all...with that said you should definitively use javascript or jQuery for this type of thing:

HTML

<!DOCTYPE html>  <html>   <head>      <meta charset="ISO-8859-1">      <title>Online Student Portal</title>   </head>   <body>      <form action="">          <input type="button" id="myButton" value="Add"/>     </form>   </body>  </html> 

JQuery

var button_my_button = "#myButton"; $(button_my_button).click(function(){  window.location.href='Students.html'; }); 

Javascript

  //get a reference to the element   var myBtn = document.getElementById('myButton');    //add event listener   myBtn.addEventListener('click', function(event) {     window.location.href='Students.html';   }); 

See comments why avoid inline scripting and also why inline scripting is bad

like image 20
d1jhoni1b Avatar answered Sep 24 '22 01:09

d1jhoni1b