<p>Type a number: <input type="text" id="f" size="4"></p>
<p>Type a different number: <input type="text" id="s" size="4"></p>
<button id="b" class="myButton">Get the Larger</button>
<p>The larger of the two numbers is:
<span id="write"></span></p>
// get the button and make it respond to a click
var theButton = document.getElementById("b");
theButton.onclick = feedTheButton;
// simple function compares two numbers, returns the larger one
function greatestOfTwo( first, second ) {
if ( first > second ) {
return first;
} else {
return second;
}
}
// this function runs each time the button is clicked
// the simple function is called within this one
function feedTheButton() {
// get the two numbers from the text input fields
// parseInt() changes string to number
var box1 = parseInt(document.getElementById("f").value);
var box2 = parseInt(document.getElementById("s").value);
// run the function above this one and store what is returned in result
var result = greatestOfTwo( box1, box2 );
// write the result into the HTML
var place = document.getElementById("write");
place.innerHTML = result;
}
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.