Parse selectbox value with javascript

Hey guys,

i’ve the following question:
how can i parse the value of a selectbox within a div-tag (with the id
“ma”) via AJAX and use the parsed value for reading out some data from
my database. the parsing request should be started by selecting another
selectbox.

My mainquestion is: how can i parse a selectbox (if possible with rjs)
an get the value in ruby (not just in javascript) for further actions
(like reading out some data from database)?

thank you for your response!

Tobe

Thanks for the fast request.

Do you also know how it’s possible to read all values of different
selectboxes in different div-tags (but all with the same name “algo”)
with prototype: Is there a way to loop over all those div-tags and read
the value of the selectbox in each div-tag?

On Mar 31, 2011, at 7:10 AM, Tobias H. wrote:

an get the value in ruby (not just in javascript) for further actions
(like reading out some data from database)?

You have to have JavaScript in your page which will respond to the
‘change’ event on your selectbox by sending an Ajax request to your
server. At that point, it will be a normal Rails request, and you’ll
have the values. Your server will respond with the result. There are
helpers (or used to be in Rails 2) to generate this JavaScript for
you, but it’s important to understand that you can’t (from Ruby, in
Rails) ask the browser anything. You have to be waiting for it to ask
things of you.

The Prototype-flavored JavaScript to do this would be something like
this:

$(‘mySelectBoxId’).observe(‘change’,function(evt){
new Ajax.Request(‘your/server/endpoint’,{
parameters:{mySelectBox:this.getValue()},
onSuccess:function(transport){
//do something with
transport.responseText
},
onFailure: function(transport){
//sad face
}
});
});

You can probably get similar code injected into your page for you by
one of the Rails helpers, but that’s the basic mode by which it will
work.

Walter

Hi All,

Apologies if this is verging on O/T for the list, but I do see some job
ads and I’ve been trying to contribute substantively to the list for the
last couple of months.

I’m in the horrible position of having too much work, but some of it
isn’t that profitable so I can’t go to my regular guys who are $100+/hr.
Simple content management systems, etc. All in Rails 3.0.x with Ruby
Enterprise 1.8.7 being deployed on Heroku using SendGrid, etc. I need
someone to create a good first cut of sites. They need to use rvm, use
git and create meaningful commit messages every 15 mins to 2 hours as
they complete discrete chunks of work, name their fields and classes
intelligently, keep method sizes low (most 6-8 lines, all under 20 or
so). They need to know Devise for authentication, Cancan for
authorization, Paperclip (with S3) for file uploads on Heroku and be
equally comfortable with haml and erb. They also need to naturally write
at least some acceptance tests first using cucumber and capybara, and
write some rspec tests for any areas of business complexity. I’m not
looking for high levels of coverage in rcov, but a decent effort to have
some degree of smoke testing of the app which we can upgrade over time.
Some of the apps may need a little jQuery loving, but nothing too
complex.

I have a specific project now, and then 2-3 a month coming down the
line. Anyone who feels they meet the criteria and would like to build a
relationship, please drop me a line off-list ([email protected]) with some
information about yourself and your rates. To give you an idea,
depending on your productivity this is the kind of work that would make
sense at maybe $20-25/hr assuming you can knock out sites pretty
quickly, so it’s either offshore or “beer money” work. Eventually I’d
want to just pay hourly so we’re both focused on doing things both well
and quickly, although I might give some specific well specified stories
for a fixed bid to try things out on the first project.

And now back to our regularly scheduled tech conversations - apologies
for the interruption.

Best Wishes,
Peter

Hey guys,

I tried the following (in a RJS-Template), but it doesn’t work:

page[:ma].select(‘select#makro_ma_attributes__algorithm_id’).each do
|sa|
puts sa.getValue
end

=> i want to loop over all selectboxes and read out the value with RJS

=> “ma” is a div tag which contains many other div tags
=> every div tag in “ma” contains one selectbox
=> every selectbox contains the id “makro_ma_attributes__algorithm_id”

On 31 March 2011 16:17, Tobias H. [email protected] wrote:

=> i want to loop over all selectboxes and read out the value with RJS

=> “ma” is a div tag which contains many other div tags
=> every div tag in “ma” contains one selectbox
=> every selectbox contains the id “makro_ma_attributes__algorithm_id”

It is not valid html to use an id more than once on a page. Run the
page through the w3c html validator and make sure the html is valid
before trying to get the script to run. Then run it in firefox with
the firebug addon to see if the script generates errors.

Colin

If you have multiple elements on your page that have the literal value
name=“algo” then the last one will “win” and only that one value will
be submitted to the server by the browser. If they are actually
name=“algo[]”, then you will receive an array of values for algo and
you can iterate over them on the server with each(). In Prototype, you
can get all of the values for the current form in one serialized
string like this (inside your Ajax Request):

… parameters: this.up(‘form’).serialize(), …

Then your controller method can pick out the ones you want
with :params[:algo] or something like that.

Walter