Created
March 27, 2022 17:22
-
-
Save LiorB-D/d88fdb9d101e746cb6c8bdc4fd9e03d6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> | |
<body> | |
<input id = "numInp"></input> | |
<button>Calculate Square</button> | |
<p class = "output">Enter a number</p> | |
</body> | |
<script> | |
// We want to wait for the document to be ready before running any queries | |
$(document).ready(() => { | |
// Query Button and add click event | |
$("button").click(() => { | |
// Pull input and square it | |
let num = $("#numInp").val() | |
let square = num * num | |
// If it is an invalid number give error message, else display square | |
if(isNaN(num)) { | |
$(".output").css('color', 'red') | |
$(".output").html("Invalid Input: " + num) | |
}else { | |
$(".output").css('color', 'black') | |
$(".output").html(num + " times " + num + " = " + square) | |
} | |
}) | |
}) | |
</script> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment