0

I'm try add new dropdown field when click on add button and its work properly but when i'm add some dynamic dropdown box and fill with values and click on add button for new dropdown box then all above values are removed so how can i do add without remove existing values?

Here is code

<script>
     function addNew () {
        var NumOfField = document.getElementById('fields').value; 
        NumOfField = Number(NumOfField)+1;
        document.getElementById('fields').value = NumOfField;

        document.getElementById('newEvent').innerHTML += "<br><br><br><label class='control-label col-md-3 col-sm-3 col-xs-12'>Select </label><div class='col-md-9 col-sm-9 col-xs-12'><select class='form-control' name='event"+NumOfField+"'><option value='select'>Select</option><option value='1'>foo</option></option><option value='2'>baar</option></select></div>";
    }
    </script>


   <body>
   <input type="hidden" value="1" id="fields" name="fields">
   <div class="form-group" id="newEvent">
    <label class="control-label col-md-3 col-sm-3 col-xs-12">Select Step 1</label>
    <div class="col-md-9 col-sm-9 col-xs-12">
    <select class="form-control" name="event1">
    <option value="select">Select</option>
    <option value="1">foo</option>
    <option value="2">bar</option>
    {% endfor %}
    </select>
   </div>
  </div>
 <button type="button"onclick="addNew();">Add</button>
1
  • copy your 1st dropdown html code and paste in script and assign to specific var. Then assign that var to new dropdown Commented Feb 6, 2016 at 9:13

1 Answer 1

1

It's very simple, just look on this example provided by me in the snippet.

  1. You have to connect variable to select object.
  2. Get value and text of option from the user.
  3. Create new DOM element.
  4. Configure this element.
  5. Append this element.
  6. Be proud of your working dropdown.

It will be working with bootstrap as you had in the question. But I removed all the sceleton to provide tidy answer.

Have a nice day!

<select id="example">
  <option value="select">Select</option>
  <option value="1">foo</option>
  <option value="2">bar</option>
</select>
<hr/>
Value: <input type="text" id="val" />
<br/>
Option body: <input type="text" id="text" />

<button id="button">Add new option</button>

<script>
  var anchor = document.getElementById("example")
  function addNew() {
    var anchor = document.getElementById("example")
    var val = document.getElementById("val").value
    var text = document.getElementById("text").value
    alert(val+"=>"+text)
    if (val && text) {
      alert("adding new option")
      var option = document.createElement("option")
      option.classList.add("example")
      option.classList.add("example2")
      option.name = "for php"
      option.id = "you know"
      option.value = val
      option.text = text
      anchor.appendChild(option)
    } else {
      alert("fill the fields")
    }
  }

  document.getElementById("button").onclick = addNew
</script>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.