Latihan #3 Memperkenalkan Alert pada jQuery Disertai Penggunaan Method "append'' dan Selector "$this"

Submitted by Author Site on Saturday, 1 June 2019 - 20:14

Setelah belajar teori singkat tentang method append dan variabel $this pada tulisan sebelumnya, di kesempatan kali ini Kita akan melanjutkan teori tersebut melalui sebuah program yang diimplementasikan dalam dunia web.

1. Buat file index.html

<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
        integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <link rel="stylesheet" href="style.css ">
    <script src="../jquery/jquery-3.4.0.min.js"></script>
    <script src="alerts.js"></script>
    <title>JQuery di Dunia Web</title>
</head>

<body>
    <br>
    <div class="container mt-4 text-center display-4">
        <h1>Memperkenalkan Alert di jQuery (append dan this)</h1>
        <br>
        <div class="row ">
            <div class="card col-md-3 dia" style="width: 18rem;">
                <img src="img/dia-huiyeon.jpg" class="card-img-top" alt="...">
                <div class="card-body">

                </div>
            </div>
            <div class="card col-md-3 dia" style="width: 18rem;">
                <img src="img/dia-yebin.jpg" class="card-img-top" alt="...">
                <div class="card-body">

                </div>
            </div>
            <div class="card col-md-3 dia" style="width: 18rem;">
                <img src="img/dia-chaeyeon.jpg" class="card-img-top" alt="...">
                <div class="card-body">

                </div>
            </div>
            <div class="card col-md-3 dia" style="width: 18rem;">
                <img src="img/dia-eunice.jpg" class="card-img-top" alt="...">
                <div class="card-body">

                </div>
            </div>
        </div>

    </div>

    <!-- Optional JavaScript -->

    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
        integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous">
    </script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
        integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous">
    </script>
</body>

</html>

 

2. Buat file alerts.js

    $(document).ready(function () {
        $(".dia").click(function () {
            var tinggi = Math.floor(Math.random() * (167 - 164 + 1)) + 164; // menampilkan angka acak mulai dari angka 167 hingga 164
            var tinggi_msg = "Tinggi badannya sekitar " + tinggi + " cm . Maaf kalau salah ^^"; 

            alert(tinggi_msg); // menampilkan pesan var tinggi_msg 
            $("$this").append(tinggi_msg);
        });
    });

 

3. Buat file style.css

.dia {
    padding: 25px;
    text-align: center;
    width: auto;
}