Ben Brightwell
2006-Aug-15 01:52 UTC
[Rails] Extracting the current value of a DOM element
So, say I have two select boxes. One with the letters of the alphabet, and a second with a list of names. When I select the letter in the first, how do I, in my onChange function, extract the value I selected in the first box, to determine what values to populate the second box with? I don''t want to submit the form, I''d like to do this with Ajax. Thanks in advance, Ben. -- Posted via http://www.ruby-forum.com/.
Dana Pieluszczak
2006-Aug-15 03:36 UTC
[Rails] Re: Extracting the current value of a DOM element
Ben Brightwell wrote:> So, say I have two select boxes. One with the letters of the alphabet, > and a second with a list of names. When I select the letter in the > first, how do I, in my onChange function, extract the value I selected > in the first box, to determine what values to populate the second box > with? > > I don''t want to submit the form, I''d like to do this with Ajax. > > Thanks in advance, > Ben.<script type="text/javascript"> function alertSelected(selectObj) { alert(''selected value is: '' + selectObj.options[selectObj.selectedIndex].value); } </script> <form> <select onchange="alertSelected(this)" id="letters"> <option value="a">A</option> <option value="b">B</option> </select> </form> completely untested, but you get the idea. You first need a reference to the select element. Then the selectedIndex property will give you the index of the selected option and the options property is an array of the options. Dana P -- Posted via http://www.ruby-forum.com/.
Ben Brightwell
2006-Aug-15 05:35 UTC
[Rails] Re: Extracting the current value of a DOM element
Dana Pieluszczak wrote:> <script type="text/javascript"> > function alertSelected(selectObj) { > alert(''selected value is: '' + > selectObj.options[selectObj.selectedIndex].value); > } > </script> > > <form> > <select onchange="alertSelected(this)" id="letters"> > <option value="a">A</option> > <option value="b">B</option> > </select> > </form> > > completely untested, but you get the idea. You first need a reference to > the select element. Then the selectedIndex property will give you the > index of the selected option and the options property is an array of the > options. > > Dana POk, that works for a static example, but what if I want to send the value to the controller? I can''t figure out how to take the ''this'' object and translate it to ruby code. -- Posted via http://www.ruby-forum.com/.