• Home
  • About

stonean

Feeds:
Posts
Comments

jQuery observe_field

August 5, 2008 by stonean

I’ve never been much of a Javascript fan, but thanks to jQuery, I find myself actually enjoying the times when Javascript is needed. This is nothing against Prototype, the jQuery syntax just clicks with me.

As Rails is coupled with Prototype pretty well, I didn’t want to do a complete switch with something like JRails, I wanted to mix it in… get the best of both worlds.

So, I made use of the jQuery.noConflict(); functionality and started using jQuery. My first was use was with the very nice Treeview plugin. I really like this plugin.

As I don’t like to code for purely academic purposes, I didn’t do much else with jQuery until I found a need to use an observe_field (Rails helper name) type functionality (Prototype type just calls this method “observe”). The basic idea was that I have a select input with various task types. On the “on_change” event, I wanted go to the new task page passing the task_type_id I wanted to create.

So, here’s the jQuery way to do this:

jQuery("#task_type_id").bind("change",function(){
  location.href = "/projects/<%= @project.id %>/tasks/new?task_type_id=" + jQuery(this).val();

To break this down a little:
My select tag has an id of “task_type_id”, this part finds and returns the select element.

jQuery("#task_type_id")

Now I want to bind an event to the select element. The first parameter of the bind call is the type of event you want to bind to:

.bind("change"

The second parameter to the bind call is the function you want executed when this event fires:

function(){
  location.href = "/projects/<%= @project.id %>/tasks/new?task_type_id=" + jQuery(this).val();
}

This gets the value of the select element to pass into the location.href call:

jQuery(this).val()

So now when my select box value is changed, the user is sent to the new task page with the appropriate task_type_id.

Posted in RubyOnRails, jQuery | No Comments Yet

  • Open Source


    Lockdown (GitHub)
    RuHL (GitHub)

  • Recommend Me

  • twitter: stonean

    • I checked my RailsConf proposal status and it's 'under review'. 'Dumb and Dumber' then came to mind: 'so there's a chance, *yeah*'. 19 hours ago
    • Yet another way to waste time in the guise of keeping up to date: http://vurl.me/EIR 3 days ago
    • So I submitted my RailsConf proposal, called it Beyond MVC. We shall see. I do such a bad job talking about myself, the bio sucked. 4 days ago
    • @ubermuda that's good to know, thank you! 5 days ago
    • @chanmix51 cool, thanks! 5 days ago
  • Categories

    classy-inheritance Code Style DataMapper Fusion git Haml Interview Questions jQuery lockdown Merb MysqlUtils Process Quick Tips REST RSpec Ruby RubyOnRails rubytrends Stage thoughts Uncategorized


  • Archives

    • November 2009
    • May 2009
    • April 2009
    • March 2009
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • August 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • March 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007

Blog at WordPress.com.

Theme: Mistylook by Sadish.