Skip to content

Instantly share code, notes, and snippets.

@mh108
Last active February 9, 2016 20:33
Show Gist options
  • Save mh108/a14d7f8010421f5cf54e to your computer and use it in GitHub Desktop.
Save mh108/a14d7f8010421f5cf54e to your computer and use it in GitHub Desktop.
Client-Side JavaScript: Event-Handler Demo with jQuery
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' >
<title> isEven? </title>
</head>
<body>
<h2>Button Demo with jQuery</h2>
<p>
Enter a number:
<input type="text" id="inputBox" size="12" value="">
</p>
<button>isEven?</button>
<hr style="width:23%; margin-left:0;">
<div></div>
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script src="client-side-jQ.js"></script>
</body>
</html>
"use strict";
//function accepts an argument, returns a value
function isEven(n){
if(n % 2 == 0)
return true;
else
return false;
}
//1. define the onclick handler
var main = function(){
//get value entered by user in web page
var val = $("#inputBox").val();
//display result on web page
//caveat: html() won't display booleans
$("div").html(String(isEven(val)));
};
//2. Select the button and register the handler
//button1.on("click", main);
$(document).ready($("button").on("click", main));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment