Advertisement
trainer_pemrograman

penerapan jquery event

Sep 24th, 2024
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.08 KB | Source Code | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5.     <title>Penerapan jQuery Event pada Mouse, Keyboard dan Form</title>
  6.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
  7. </head>
  8.  
  9. <body>
  10.     <h1>Halo, Selamat Datang di Gamelab!</h1>
  11.     <div id="myDiv" style="width: 200px; height: 200px; background-color: lightblue;">Arahkan kursor mouse ke sini</div>
  12.     <br>
  13.     <button id="myButton">Klik Saya</button>
  14.     <input type="text" id="myInput" placeholder="Ketik sesuatu di sini">
  15.     <p id="hasil_input"></p>
  16.     <form id="myForm">
  17.         <label for="nameInput">Nama:</label>
  18.         <input type="text" id="nameInput"><br>
  19.         <label for="emailInput">Email:</label>
  20.         <input type="email" id="emailInput"><br>
  21.         <input type="submit" value="Submit">
  22.     </form>
  23.     <p id="hasil_form"></p>
  24.     <script>
  25.         $(document).ready(function () {
  26.             //Penerapan untuk tag h1
  27.             $("h1").css("color", "blue");
  28.  
  29.             //Penerapan Event Mouseenter dan Mouseleave
  30.             $("#myDiv").mouseenter(function () {
  31.                 $(this).text("Kursor Mouse berada di dalam");
  32.             });
  33.  
  34.             $("#myDiv").mouseleave(function () {
  35.                 $(this).text("Kursor Mouse berada di luar");
  36.             });
  37.  
  38.             //Penerapan event ketika tombol diklik
  39.             $("#myButton").click(function () {
  40.                 alert("Tombol diklik!");
  41.             });
  42.             //Penerapan Event Keyboard Keypress
  43.             $(document).keypress(function (event) {
  44.                 var key = String.fromCharCode(event.which);
  45.                 $("#hasil_input").text("Anda menekan tombol " + key);
  46.             });
  47.             // Penerapan Event Form submit
  48.             $("#myForm").submit(function (event) {
  49.                 event.preventDefault(); // Menghentikan submit form
  50.                 var name = $("#nameInput").val();
  51.                 var email = $("#emailInput").val();
  52.                 $("#hasil_form").text("Nama: " + name + ", Email: " + email);
  53.             });
  54.         });
  55.     </script>
  56. </body>
  57.  
  58. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement