0

I'm using ajax function to send select values to my php script, it is working fine but i have issue when there user add multiple data, i have option to add multiple add child in the form, user can add as many fields. I need a each loop here. This is my code.

 <select class="firstpiller" name="firstpiller">
        <option value="0">0</option>
        <option value="1">1</option>
     </select>

     <select class="thirdpiller" name="thirdpiller">
        <option value="2">2</option>
        <option value="3">3</option>
     </select>

       if (document.querySelector('[name=firstpiller]') != null){
        var firstpiller = document.querySelector('[name=firstpiller]').value;
        } else {
           var firstpiller = null; 
        }


        if (document.querySelector('[name=thirdpiller]') != null){
        var thirdpiller = document.querySelector('[name=thirdpiller]').value;
        } else {
           var thirdpiller = null; 
        }


        var stepVar = firstpiller + "--" + thirdpiller;

Can anyone help how should i write each loop here?

Thanks in advance.

Zain
  • 662
  • 9
  • 26
  • [What's the best way to loop through a set of elements in JavaScript?](https://stackoverflow.com/questions/157260/whats-the-best-way-to-loop-through-a-set-of-elements-in-javascript) – Mohamed-Yousef Mar 26 '18 at 20:19

1 Answers1

2

add a common class to the select say you are given a class .myselect, now you can do something like below

var stepvar = '';
$('.myselect').each(function(value){
    stepvar += '--' + value.val(); 
});
alert(stepvar);
fayis003
  • 680
  • 4
  • 10