I need help figuring out what to do under the todo comments below. var calculate
ID: 645459 • Letter: I
Question
I need help figuring out what to do under the todo comments below.
var calculateMpg = function () {
// Below is jQuery's way of getting an input element's value.
// Compare/contrast it with DOM API in previous version of mpg_with_style.html
var miles = parseFloat($("#miles").val());
// TODO: Fill in the rest of the body of function.
// Hint: to set an element's value use
// $("#id").val(new value);
};
// jQuery uses $(document).ready instead of windows.onload.
// There is a big difference here! ready is a function that takes a function parameter.
// onload is a property that is assigned a function value. Subtle, but important difference!
$(document).ready( function () {
// Line below attaches a click handler to the element selected (the calculate button).
// Compare/contrast it with the DOM API in mpg_with_style.html.
$("#calculate").click(calculateMpg);
$("#miles").focus();
});
</script>
</head>
<body>
<section>
<h1>Calculate Miles Per Gallon</h1>
<label for="miles">Miles Driven:</label>
<input type="text" id="miles"><br>
<label for="gallons">Gallons of Gas Used:</label>
<input type="text" id="gallons"><br>
<label for="mpg">Miles Per Gallon</label>
<input type="text" id="mpg" disabled><br>
<label> </label>
<input type="button" id="calculate" value="Calculate MPG"><br>
</section>
</body>
</h
Explanation / Answer
var calculateMpg = function () { // Below is jQuery's way of getting an input element's value. // Compare/contrast it with DOM API in previous version of mpg_with_style.html var miles = parseFloat($("#miles").val()); var gallons = parseFloat($("#gallons").val());//getting the value of gallons of gas used var mpg=miles/gallons; //calculating the miles per gallon $("#mpg").val(mpg); //setting the value of text field // TODO: Fill in the rest of the body of function. // Hint: to set an element's value use // $("#id").val(new value); }; // jQuery uses $(document).ready instead of windows.onload. // There is a big difference here! ready is a function that takes a function parameter. // onload is a property that is assigned a function value. Subtle, but important difference! $(document).ready( function () { // Line below attaches a click handler to the element selected (the calculate button). // Compare/contrast it with the DOM API in mpg_with_style.html. $("#calculate").click(calculateMpg); $("#miles").focus(); });
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.