0

I have a dynamic html code that populate firstname & lastname, it also add a column of drop down selection of Gender and text input field for them to report income and car model. The list of names sometime can be just 2 rows or 50 rows depending on which data it is reading from. the html look as below..

<tr><td style='width:50px; class='reg' value=''>+FName+</td>
            <td style='width:50px; class='reg' value=''>+LName+</td>
            <td style='width:50px; class='reg' id='gend'>
               <select>
                  <option value='F'>Female</option>
                  <option value='M'>Male</option>
              </selected></td>
           <td style='width:50px; class='reg' value=''><input class='inp' type='text'></td>
           <td style='width:50px; class='reg' value=''><input class='inp' type='text'></td>
     </tr>

my Jquery looks like

cap=$("#gend option:selected").map(function(){
return $(this).val();
}).get().join(",");
})

The query partially works because it is only captures the first row dropdown. if I have 2 + rows it only capture the first drop down selected only. is there a way to capture all rows selected from dropdown? when I change the Jquery from option:selected to option, I can see all the options for each row. Basically if I have 3 rows I see both F,M three times. I am not sure what I am missing? Also can I make one of the option as default?

GMark
  • 1
  • 1
  • You're duplicating the same `id` in the DOM when they *have* to be unique. This is why only the first row is found. Change the `gend` id to a class, and amend the JS to select this class and your code will work as it is. – Rory McCrossan Feb 05 '20 at 14:20
  • 1
    yes, that did the trick. Thanks a lot Rory – GMark Feb 05 '20 at 14:33
  • what about if I want to get the selected drop down list plus texts from the input field ? – GMark Feb 05 '20 at 19:44
  • Use DOM traversal methods such as `closest()` and `find()` to retrieve the `input` related to the `select` which raised the `change` event. If you're stuck with this, try asking a new question about it – Rory McCrossan Feb 06 '20 at 08:50

0 Answers0