javascript - add more form element logic with radio input -


i wrote code user can click add more duplicate form elements , add values. it's working fine except radio inputs. stuck. how can solve problem in easy way? below code.

<div id="dup">     <p>         athlete name:<br>         <select name="athlete_name" id="athlete_name[]">             <option value="" selected="selected">choose user</option>             <option value="2">candice falzon</option>             <option value="5">athlete example</option>         </select>     </p>      <p>         sex:          <input type="radio" name="athlete_sex" value="male">male          <input type="radio" name="athlete_sex" value="female">female     </p> </div> 

i can not take athlete_sex array .

sometimes jquery can stickler closing tags. fixed them , works:

    var clone = $('#dup:last').clone();        window.addanother = function() {        clone.find('input').attr('name', 'athlete_sex' + ($('#wrapper').children().length + 1));        $('#wrapper').append(clone);        clone = clone.clone();      }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div id="wrapper">    <div id="dup">      <p>athlete name:        <br />        <select name="athlete_name" id="athlete_name[]">          <option value="" selected="selected">choose user</option>          <option value="2">candice falzon</option>          <option value="5">athlete example</option>        </select>      </p>        <p>sex:        <input type="radio" name="athlete_sex" value="male" />male        <input type="radio" name="athlete_sex" value="female" />female</p>    </div>  </div>  <button onclick="addanother()">add another</button>


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -