0

I am trying to change a value of a select field by selecting a value of another field with the help of jquery.

The event fires, but does nothing. Not even return an error:

<select name="attr[8]" id="attr[8]" onchange="$('#attr[5]').val(157);">
      <option><!-- some stuff --></option>
    </select>
    <select name="attr[5]" id="attr[5]">
  <option value="">auswählen</option>    
        <option value="157">Herrenuhr</option>
        <option value="11">Damenuhr</option>  
 </select>

This does not work as well:

<select name="attr[8]" id="attr[8]" onchange="$('#attr\\[5\\]').val('157');">
</select>

Is there a way to change the selected value of the second select box with the html onchange event instead of the jquery "change" event?

merlin
  • 2,717
  • 3
  • 29
  • 59
  • first thing 157 should be inside single '' quotes and what is problem in using Jquery change event? – chandra shekhar joshi Mar 14 '19 at 09:41
  • Tried out both, escaping brakets and also adding single quotes. No change at all. I don't want to add a javascript function inline of html due to rendering/speed concerns. Would is your recommendation? – merlin Mar 14 '19 at 10:00
  • @Rory This is not a dupplicate although it looks like it. The solution is to add not only the brackets, but also a change event: $('#attr\\[5\\]').val(157).change(); – merlin Mar 14 '19 at 10:23
  • So you need to raise a jQuery change event within the change event handler? That seems like a very bad idea. – Rory McCrossan Mar 14 '19 at 10:27

0 Answers0