Skip to content
Advertisement

Duplicate inputs when append using jQuery

Here is the code:

    <form class="ui form attached fluid loading segment" onsubmit="return contact(this)">
    <div class="field">
    <label>Preferred Tour</label>
        <div class="field">
          <?php
            $conn=mysqli_connect('####','####','####','####');

            echo '<select required id="tourInfo">';
            echo '<option value="" selected disabled>--Preferred Tour--</option>';
            $db = mysqli_query($conn, "SELECT `id`,`tourprice`,`tourname` FROM `available_tours`");
            while ($d=mysqli_fetch_assoc($db)) {
              
              echo "<option value=".$d['id']." id=".$d['tourname']. " data-price=".$d['tourprice'].">".$d['tourname']."</option>";
            
            }
              
            echo "</select>";
          ?>
        </div>   
    </div>
    <div class="field" id="hiddenTortu" style="display: none;"> 
    <label>Attention</label> 
        <div class="ui icon">
          <p><b>The minimum of people for this tour is 5, less than 5 the tour is not realisable</b></p>
        </div>
    </div>
    <div class="field">  
      <label>Available Time</label>
      <div class="field">
        <?php
          $conn=mysqli_connect('####','####','####','####');

          echo '<select name="gender" required id="timeInfo">';
          echo '<option value="" selected disabled>--Preferred Time--</option>';
          $db = mysqli_query($conn, "SELECT `time_real` FROM `available_time`");
          while ($d=mysqli_fetch_assoc($db)) {
            echo "<option value=".$d['time_real'].">".$d['time_real']."</option>";
          }
            
          echo "</select>";
        ?>
      </div>
    </div>
  <div class="two fields"> 
      <div class="field" id="pax"> 
          <label>Please specify the number of People according to the perred tour selection</label>

Here starts the problem with the following script according to the tour selection I’m trying to set up a minimum and maximum so that the user can’t choose more numbers for the people on the tour.

The problem is that when the user select first one option, and then realised that the best option is another one, when he/she does another selection the input created with jQuery that was appended remains and because of the new selection a new input is created.

The intention is that if the user chooses option 1 the input append appears according to option one, but if the user regrets and prefers option 2 that the input for the option 1 disappears and a new input according to option 2 appears and so on for the entire if conditions.

          <script>
                $(document).ready(function(){
                  $('#tourInfo').on('change', function() {
                    if ( this.value == '1')
                    {
                      $("#pax").append($('<input placeholder="Number of People" type="number" id="peopleInfo" min="1" max="2"  value="1" required>'));
                                  (function ($) {
                          $.fn.restrict = function () {
                              return this.each(function(){
                                  if (this.type && 'number' === this.type.toLowerCase()) {
                                      $(this).on('change', function(){
                                          var _self = this,
                                              v = parseFloat(_self.value),
                                              min = parseFloat(_self.min),
                                              max = parseFloat(_self.max);
                                          if (v >= min && v <= max){
                                              _self.value = v;
                                          }
                                          else {
                                              _self.value = v < min ? min : max;
                                          }
                                      });
                                  }
                              });
                          };
                      })(jQuery);

                      $('#peopleInfo').restrict();

                    }
                    else if (this.value == '2')

                      $("#pax").append($('<input placeholder="Number of People" type="number" id="peopleInfo" min="3" max="5"  value="3" required>'));
                                  (function ($) {
                          $.fn.restrict = function () {
                              return this.each(function(){
                                  if (this.type && 'number' === this.type.toLowerCase()) {
                                      $(this).on('change', function(){
                                          var _self = this,
                                              v = parseFloat(_self.value),
                                              min = parseFloat(_self.min),
                                              max = parseFloat(_self.max);
                                          if (v >= min && v <= max){
                                              _self.value = v;
                                          }
                                          else {
                                              _self.value = v < min ? min : max;
                                          }
                                      });
                                  }
                              });
                          };
                      })(jQuery);

                      $('#peopleInfo').restrict();
                    }
                    else if (this.value == '3')
                    {
                      $("#pax").append($('<input placeholder="Number of People" type="number" id="peopleInfo" min="6" max="15"  value="6" required>'));
                                  (function ($) {
                          $.fn.restrict = function () {
                              return this.each(function(){
                                  if (this.type && 'number' === this.type.toLowerCase()) {
                                      $(this).on('change', function(){
                                          var _self = this,
                                              v = parseFloat(_self.value),
                                              min = parseFloat(_self.min),
                                              max = parseFloat(_self.max);
                                          if (v >= min && v <= max){
                                              _self.value = v;
                                          }
                                          else {
                                              _self.value = v < min ? min : max;
                                          }
                                      });
                                  }
                              });
                          };
                      })(jQuery);
                      $('#peopleInfo').restrict();
                    }...
                    
                              ...});
                          };
                      })(jQuery);

                      $('#peopleInfo').restrict();
                    }
                  });
            });
        </script>
      </div> 
      <div class="field"> 
          <label><br>Date of Tour</label>
          <input type="text" readonly required id="tourDate" class="datepicker-here form-control" placeholder="ex. August 03, 1998">
      </div>  
  </div>
  <div style="text-align:center">
    <div>
      <label>Ensure all details have been filled correctly</label>
    </div>
    <button class="ui green submit button">Submit Details</button>
  </div> 
</form>
</div>

Advertisement

Answer

Move your script out from inside the div with id pax, then Clear your html of the element with id pax before appending:

 //Using JQuery
 $('#pax').html('');
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement