Skip to content

Instantly share code, notes, and snippets.

@ballauriena
Forked from dbc-challenges/lucky_ajax.md
Last active August 29, 2015 13:55
Show Gist options
  • Save ballauriena/8702097 to your computer and use it in GitHub Desktop.
Save ballauriena/8702097 to your computer and use it in GitHub Desktop.
$(document).ready(function () {
// PSEUDO-CODE:
// 1- intercept the form submission event using jQuery
// 2- prevent the default action for that event from happening
// 3- generate a random number between 1 and 6 using JavaScript
// 4- use jQuery to submit an AJAX post to the form's action
// 5- when the AJAX post is done, replace the contents of the "#die" DIV in the DOM using jQuery
// event.preventDefault();
$("form").submit(function(){
$num = Math.floor((Math.random()*6)+1);
$("#die").replaceWith(function(){
event.preventDefault();
$.post( "/rolls", $num, function(event){
$("#die").html(event);
});
});
});
});

Instructions:

  1. Download this application skeleton.
  2. Convert the app to use AJAX.
  3. Add any files you changed to your gist and submit your code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment