0

in rails, i have a nested attribute for selection, different dropdown of same id & name. I'm forming an array for each id of selected options through the javascript function 'hoho'. There is no blank option so the options should be selected once page is loaded.

i wanted to immediately form the array once i navigated into the dropdown page, but i only get the array by reloading the page.

is it due to the onload code or turbolink issue with rails? seen somewhere maybe it is related to some page:change code instead?

please advise.. im using mozilla if that will be helpful, thanksss

rails html view

<select name="variant[option_value_ids][]" onchange="hoho()" id="variant_option_value_ids">
<option value="69">apple</option>
<option value="70">orange</option>
</select>

<select name="variant[option_value_ids][]" onchange="hoho()" id="variant_option_value_ids">
<option value="69">apple</option>
<option value="70">orange</option>
</select>

javascript

function hoho() {
   var foo = [];
   $("[id^=variant_option_value_ids] option:selected").each(function(){
    foo.push($(this).val());
    });
}

window.onload = hoho;
ckeat9
  • 162
  • 1
  • 14
  • Can you running that '$("[id^=variant_option_value_ids] option:selected")' selector in a loaded page using the web inspector / firebug? Do you get your selected options? – mlabarca Dec 26 '16 at 14:00
  • Also, try using jquery's `document.ready` event instead of the standard window.onload. Onload apparently tends to work a bit different in different browsers, and it waits until images are loaded which you shouldn't need. Read on: http://stackoverflow.com/questions/3698200/window-onload-vs-document-ready – mlabarca Dec 26 '16 at 14:05

0 Answers0