ฟังก์ชัน JavaScript alert() ใช้เพื่อแสดงข้อความป๊อปอัปแก่ผู้ใช้
ต่อไปนี้เป็นรหัสสำหรับฟังก์ชัน JavaScript alert() -
ตัวอย่าง
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
</style>
</head>
<body>
<h1>Javascript text alert</h1>
<button class="Btn">CLICK HERE</button>
<h3>Click on the above button to trigger the alert popup</h3>
<script>
let BtnEle = document.querySelector(".Btn");
BtnEle.addEventListener("click", () => {
alert("You pressed the button");
});
</script>
</body>
</html> ผลลัพธ์
รหัสข้างต้นจะสร้างผลลัพธ์ต่อไปนี้ -

เมื่อคลิกปุ่ม 'คลิกที่นี่' -
