Create multiple select options with Jquery

Asked

Viewed 32 times

0

Good afternoon, what I’m trying to do is a button where clicking creates a select option, but I can create as many times as I want.

my select with database connection;

<select name="produto_tipo">
  <?php while($row2=$stmt3->fetch(PDO::FETCH_BOTH)):;?>
  <option value="<?php echo $row2[0];?>"><?php echo $row2[1];?><?php echo $row2[2]."$"; ?></option>
  <?php endwhile;?>
</select>

my button;

<button  class='w3-bar-item w3-button'>Inserir Produto</button>

The Jquery part I don’t know how to do already I’ve been searching on the net I didn’t find what I wanted, only create new inputs and texts but tried to implement and came to nothing.

If there’s a better alternative I’d like to know.

  • You want to create an extra option within the select option or an extra select option with each click?

1 answer

1

Can only be done using javascript.

  var button = document.querySelector("#button");
  var select = document.querySelector("#select");
  button.onclick = adicionaOption;

  function adicionaOption(){
    var option = document.createElement('option');
    var textOption = document.createTextNode('option');
    option.appendChild(textOption);
    select.appendChild(option);
  }
<select name="" id="select">
    <option value="">teste</option>
  </select>
  <button id="button">Add</button>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.