Forum: Ruby on Rails select with onChange

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F466de503c3095e452701c2d6e4e1112?d=identicon&s=25 oozy (Guest)
on 2007-07-06 05:33
(Received via mailing list)
Hi,

I'm trying to use a form.select with a a simple javascript alert.
This is my code.
<%= form.select(:entry_type, Entry::TYPES, :prompt => "Select The
Type", :onChange => "alert('test')") %>

The form loads without any issue, but onChange is not in the HTML
source.  Of course, since the onChange is not in the source, nothing
happens regardless of the list item I select.

Does anybody know what is going on?  Why is the onChange not appearing
in the source?

Thanks
0900e6a4828bd989f96427082c6c74ca?d=identicon&s=25 Mike Garey (random52k)
on 2007-07-06 05:49
(Received via mailing list)
the format for the select helper is the following:

select(object, method, choices, options = {}, html_options = {})

so you should use something like:

<%= select(object, method, choices, { :prompt => "Select the Type"}, {
:onchange => "alert('test');"}) %>

Adam
Aafa8848c4b764f080b1b31a51eab73d?d=identicon&s=25 Phlip (Guest)
on 2007-07-07 13:51
(Received via mailing list)
oozy wrote:

> I'm trying to use a form.select with a a simple javascript alert.
> This is my code.

> <%= form.select(:entry_type, Entry::TYPES, :prompt => "Select The
> Type", :onChange => "alert('test')") %>

Here's a working onChange under a different Rails function:

        <%=
          select( :user, :fighter_image_url, images,
                  { :selected => current_user.fighter_image_url },
                  { :onchange => '$("fighter_image_reflector").src =
"/images/fighters/"+this.value;' } )
        %>

One of the major joys and challenges of Rails is how many different ways
it
supports to pass raw HTML attributes into methods that generate HTML.
It's a
credit to Ruby's infinite flexibility that Rails can support so many.

For example, the above method doesn't take just one trailing hash. (A
trailing hash is how most :foo => :bar magic works, without {}). In this
case, Rails takes some fixed arguments, then takes two hashes, one for
dynamic options and the other for raw HTML arguments.

Research the prototype to your select, and determine which argmument
gets
the :onchange.

--
  Phlip
  http://www.oreilly.com/catalog/9780596510657/
  "Test Driven Ajax (on Rails)"
  assert_xpath, assert_javascript, & assert_ajax
5aea8e3b710c9bdf30a43ea6c130fcbe?d=identicon&s=25 Suhas Nehete (suhaspnehete)
on 2007-09-26 16:54
how to use :onchange with select box in following case ?

<%= select('contact', 'city', [ 'Mumbai', 'Paris', 'London'], {
:onchange => "my_fun();" }) %>

here 'my_fun()' is my javascript function.


Any help ?


--
Thanks,
SN.
75fb908bf2cda96ba7cdfbe4535dcde6?d=identicon&s=25 William Pratt (Guest)
on 2007-09-26 18:59
(Received via mailing list)
just do:

<%= select('contact', 'city', [ 'Mumbai', 'Paris', 'London'], :onchange
=> :my_fun) %>



Suhas Nehete wrote:
>
> --
> Thanks,
> SN.
>

--
Sincerely,

William Pratt
This topic is locked and can not be replied to.