Skip to content

Instantly share code, notes, and snippets.

@ezy023
Forked from dbc-challenges/lucky_ajax.md
Last active December 17, 2015 08:28
Show Gist options
  • Select an option

  • Save ezy023/5579849 to your computer and use it in GitHub Desktop.

Select an option

Save ezy023/5579849 to your computer and use it in GitHub Desktop.
$(document).ready(function () {
$("form").submit(function(click) {
click.preventDefault();
var action = $(this).attr("action")
var roll = Math.floor(Math.random()*6)+1;
console.log("roll before ajax", roll);
// var method = $(this).attr("method")
$.post(action,{"value" : roll }, function(rod_name){
var roll_html = $(rod_name).find("#die").html();
console.log("roll html after ajax", roll_html);
$("#die").html(roll_html);
} );
// body...
});
// 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