If my understanding is correct, you are looking to finalize the code above in order for the largest()
function to be invoked to determine the largest number between the inputs with id="N"
and id="M"
.
One strategy could involve adding a <button>
element and utilizing jQuery to attach a click()
handler to it that would execute largest()
as shown below:
function largest() {
var num1, num2;
/* Updating to utilize jQuery style selectors */
num1 = Number($("#N").val());
num2 = Number($("#M").val());
if (num1 > num2) {
window.alert(num1 + " from N is the largest, and " + num2 + " from M is the smallest");
} else if (num2 > num1) {
window.alert(num2 + " from M is the largest, and " + num1 + " from N is the smallest");
}
}
/* Getting the find-largest button and attaching a click event listener
that invokes the largest() function */
$("#find-largest").click(function() {
/* Calling the largest() function */
largest();
/* Preventing the default behavior of the button */
return false;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js">
</script>
<div>
<label>Number N:</label>
<input id="N" type="number" />
</div>
<div>
<label>Number M:</label>
<input id="M" type="number" />
</div>
<!-- Add a button that triggers the largest() function when clicked -->
<div>
<button id="find-largest">Find largest</button>
</div>