↓ Archives ↓

Posts Tagged → jquery

jQuery Submit

When you submit a form, you might want to do something before the form is submitted. In the code below, we show an alert box with the value of the text field ‘name’.

$('#my-form').submit(function() {
  alert($('#name').val());
});

This post by Christopher Rigor is from crigor.com.

Tax for the poor and mathematically challenged

lotto simulator

The Lottery: another way for the government to tax the (typically tax-exempt) poor.

As much as I’d like to shy away from yet-another-”greed is evil” post, recent circumstances have led me to mingle with people who still think spending money on the lottery is a good idea. So instead of being a jerk IRL, I’ll just post the arguments against the lottery here online.

The first argument against the lottery is that the odds are stacked waaaaay against you. Anyone who has even the basic knowledge of Combinatorics and ROI knows that the lottery is a loser’s game.

Don’t believe me? Why not try this little app I made using Rails 3 and a dash of jQuery. You could even check the source code to see if the drawing algorithm is sound.

(It’s not an original idea, though. I just based it on a Hacker News link last week.)

The second argument is that the people likely to win the lottery are the people least likely to have the financial literacy to handle such a large amount. But I guess you already knew that.

This post by Bryan Bibat is from existence, refactored.

How to Use jQuery For Rails 3

jQuery is the defacto Javascript framework used by happy programmers. I am very glad to use it in almost all of the applications I am working on.

There are many posts on how to use it for Rails 3. But here's another one which may help Rails 3 newbies (aren't we all newbies when it comes to Rails 3?).

The jQuery Rails gem

In your Gemfile, include the jquery-rails gem created by Andre Arko of Engine Yard.

gem "jquery-rails"

And install the gem using the command:


bundle install

Install jQuery. The following command will remove prototype and scriptaculous files and add jQuery to your application:

rails generate jquery:install
or
rails g jquery:install

Using livequery

So you probably know that the following code will no longer work:

<= link_to_remote 'delete', :url => admin_image_path(image), :confirm => 'Are you sure you want to delete this image?', :method => :delete >

They probably realized there's way too much javascript on the body tags making Rails apps look ugly when you view the source.

There's a need to include livequery within the head tag or before closing body tag if you wish to follow the YSlow recommendations.


On application.js 

$('a[data-remote=true]').livequery('click', function() {
  return request({ url : this.href });
});
 

$('form[data-remote=true]').livequery('submit', function() {
  return request({ url : this.action, type : this.method, data : $(this).serialize() });
});

$(function() {
  $(".alert").click(function() {
    alert(this.getAttribute("data-confirm"));
    return false;
  })
})


Replace the link_to_remote code with the following: 

<%=link_to "Delete", admin_image_path(image), :method=>:delete, :confirm=>"Are you sure you want to delete this image?", :remote=>:true, :class=>"alert"%>

That is it! Advantages of this code over the previous code is obvious. It's not obtrusive and it works. Congratulations to all of us. We're no longer living in caves and eating whatever exists in the wild. We actually have a choice.

This post by Katz Pe is from BridgeUtopia.

jQuery to prevent form submission via ENTER

Note to self: