Skip to content

Instantly share code, notes, and snippets.

@RainMonster
Forked from dbc-challenges/lucky_ajax.md
Last active December 21, 2015 02:39
Show Gist options
  • Select an option

  • Save RainMonster/6236342 to your computer and use it in GitHub Desktop.

Select an option

Save RainMonster/6236342 to your computer and use it in GitHub Desktop.
$(document).ready(function () {
$('form').submit( function(e) {
e.preventDefault();
var value = Math.floor(Math.random() * 6 + 1);
console.log('the client side value was: ' + value);
var request = $.ajax({
url: this.action,
type: this.method,
data: { value: value }
});
request.done( function(data) {
var die = $('#die img');
die.attr('src', '/' + data + '.png');
die.attr('title', data);
$('#die').show();
});
});
});
<div class="container">
<h1>Simplest Possible AJAX</h1>
<p>This contrived app will simulate a roll of a 6-sided die.</p>
<form method="post" action="/rolls">
<input type="submit" value="Roll the Die">
</form>
<div id="die" style='display: none'>
<img alt="the roll">
</div>
</div>
get '/' do
erb :index
end
post '/rolls' do
params[:value]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment