Skip to content
Advertisement

Dynamic dropdown selection for dynamic rows not just first row

I have created a form for entering product data which sends the results back to the mysql database. I have a dropdown/select id named ‘attribute_name’ and the other ‘attribute_value’ I have managed to send the results back to the database and that is working great. I would however like to restrict the user to only choosing values based on the name so if they selected size they would only have the values of small, medium, large and not black, camo, purple from colour and vice versa.

The script at the bottom works on the first row but not on any other rows as shown in the screenshots.

Working

enter image description here

Not working

enter image description here

Code

JavaScript

This code is edited for use on stackoverflow

JavaScript

Advertisement

Answer

  1. First of all, you are not allowed to use duplicate ID. You should use class instead. Check this answer Class vs ID.

  2. Secondly, I’ve changed your change event with .on('change') which will work for dynamically added select. Check this sample JQuery .on(‘change’).

  3. Lastly, you don’t want to select all select with class attribute-name, you need to find the select on that particular row. You can do this by using .closest(‘tr’) selector to select the current row <tr> where the event happened, and then use **.find('.attribute-value')** which will traverse and find element with the class attribute-value`.

From the documentation:

  • The .closest selector traverses up the DOM to find the parent that matches the conditions.
  • The .find selector traverses down the DOM where the event occurred, that matches the conditions.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement