Skip to content

Instantly share code, notes, and snippets.

@thaenor
Created February 25, 2015 15:43
Show Gist options
  • Save thaenor/79ad862ab4ec47de7747 to your computer and use it in GitHub Desktop.
Save thaenor/79ad862ab4ec47de7747 to your computer and use it in GitHub Desktop.
jQuery example to update a form on focus
/*Content originally from CodeSchool's "try jQuery" lesson. http://try.jquery.com/levels/4/challenges/16 */
$(document).ready(function() {
/*
* this first part fetches the price and multiplies for the number input in the form
*/
$("#nights").on("keyup", function() {
var nights = +$(this).val();
var dailyPrice = +$(this).closest(".tour").data("daily-price");
$("#total").text(nights * dailyPrice);
$("#nights-count").text($(this).val());
});
/*
* This is a reset, whenever the form is focused the value is reset to 7
*/
$("#nights").on("focus", function() {
$(this).val(7);
});
});
<!-- Content originally from CodeSchool's "try jQuery" lesson. http://try.jquery.com/levels/4/challenges/16 -->
<div class="tour" data-daily-price="357">
<h2>Paris, France Tour</h2>
<p>$<span id="total">2,499</span> for <span id="nights-count">7</span> Nights</p>
<p>
<label for="nights">Number of Nights</label>
</p>
<p>
<input type="number" id="nights" value="7">
</p>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment