SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    UK
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Adding html options to a date_select field??

    I have a date select field as follows:

    Code:
    <%= f.date_select (:date_taken_out, {:start_year => 1995, :use_short_month => true,
    		:discard_day => true, :include_blank => true, :order => [:month,:year]} ) %>
    I need to add some javascript to this as html option but cant work out how to do it. I've tried everything!

    I think it should look something like this. You can see the javascript I am trying to add but obviously it does not work.

    Code:
    <%= f.date_select (:date_taken_out, {:start_year => 1995, :use_short_month => true, :discard_day => true, :include_blank => true, :order => [:month,:year]}, {:onblur=> "$('date_help').fade({ duration: 0, from: 1, to: 0 });" }) %>

  2. #2
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A) The "rails way" for adding js is actually bad js coding practice. The best way to do this is unobtrusively, by writing a separate js file and in it attaching event listeners to the fields you want the js to work with, rather than cluttering up the html with inline scripts, the way rails wants to. (That's my biggest beef with rails. Left to its own devices it will insist on a bad javascript coding practices all the way through it.)

    B) If you're going to do it this way, then you're going to have to give me more than "obviously it does not work" because when I copied and pasted your proposed line of ruby code into a working page of my own, I got the right html and javascript in the file so the ruby code obviously worked for me. If it's not doing what you want, it may be a javascript issue, not a ruby issue. Load the page and do a view source to see if the "onblur" event code is on each of the date selectors. If it is, the problem is the javascript, not the ruby, so you need to look there.

  3. #3
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Following up from Arlen's post: JavaScript is often harder to debug than Ruby because it often fails silently. I'd suggest you use Firefox with the Firebug add in to help you track down any problems. That should help you diagnose where the problem is.

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2006
    Location
    UK
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replys.

    Arlen, the ruby code works fine but when you view the source, you can see that the javascript that I want to place in the HTML options is not there.

    The thing I am having trouble with is actually placing something in the html options for a date_select. Nothing I try seems to work.

  5. #5
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, can you post more code than just that one line? Because that line created the correct js for me.

    How about you post the view and then paste in the "view source" from the browser so I can see exactly what you're doing and what you're seeing. Maybe I can duplicate it then, and be better able to help you.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •