You are on page 1of 3

Simple Javascript

1. To display time and date

<!DOCTYPE html>

<html>

<body>

<h2>My First JavaScript</h2>

<button type="button"

onclick="document.getElementById('demo').innerHTML = Date()">

Click me to display Date and Time.</button>

<p id="demo"></p>

</body>

</html>

2. Pop up dialog box

<!DOCTYPE html>

<html>

<body>

<h1>The button Element</h1>

<button type="button" onclick="alert('Hello world!')">Click Me!</button>


</body>

</html>

Prompt Box

<!DOCTYPE html>

<html>

<body>

<h2>JavaScript Prompt</h2>

<button onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script>

function myFunction() {

let text;

let person = prompt("Please enter your name:", "Alexis Wong");

if (person == null || person == "") {

text = "User cancelled the prompt.";

} else {

text = "Hello " + person + "! How are you today?";

document.getElementById("demo").innerHTML = text;

</script>
</body>

</html>

You might also like