Skip to content

Instantly share code, notes, and snippets.

@LiorB-D
Created March 27, 2022 17:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LiorB-D/d88fdb9d101e746cb6c8bdc4fd9e03d6 to your computer and use it in GitHub Desktop.
Save LiorB-D/d88fdb9d101e746cb6c8bdc4fd9e03d6 to your computer and use it in GitHub Desktop.
<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