Skip to content

Instantly share code, notes, and snippets.

@tararoys
Forked from dbc-challenges/lucky_ajax.md
Last active December 27, 2015 02:09
Show Gist options
  • Save tararoys/7249976 to your computer and use it in GitHub Desktop.
Save tararoys/7249976 to your computer and use it in GitHub Desktop.
$(document).ready(function () {
$("#diceroller").submit(function(){
event.preventDefault();
diceroll = Math.floor(Math.random() * 6) + 1;
console.log(diceroll);
// var form_data = $("#diceroller").serialize();
var form_data = {"value": diceroll};
console.log(form_data);
$.post("/rolls", form_data, function(response){
console.log(response);
$("#die").html(response);
});
});
// 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
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment