How to add value into option using javascript

FENR1R Ace

Here is my code for js

function addTag(tag) {
  var tags = document.getElementById("tags");
  var span = document.createElement("span");
  span.textContent = tag.value;
  tag.value = "";
  span.setAttribute("onclick", "this.remove()");

  tags.append(span);
}

function addOption() {
  var x = document.getElementById("tag");
  var option = document.createElement("option");
  option.textContent = tags.textContent;
  x.add(option);
}

Once the user click on the tags it will close can should display the value to the option but, I only manage to create a blank space inside the option.

Here is related JS Fiddle

kewlashu

Changed tags span to contain all option spans.

Then made each span inside tags responsible to add itself back to select and remove itself from tags span.

Please see the changes in the code as per above logic.

function addTag(tag) {
  var tags = document.getElementById("tags");
  var span = document.createElement("span");
  span.textContent = tag.value;
  tag.value = ""; //clear the field when choose or pressed enter key
  span.style.backgroundColor = "#E5E6E7";
  span.style.margin = " 5px";
  span.style.padding = "5px";

  //span will add itself back to select and remove itself from tags
  span.setAttribute("onclick", "addOption(this)");

  if (span.textContent == "Fast Food") {
    $('option[value="Fast Food"]').remove();
  } else if (span.textContent == "Vegan") {
    $('option[value="Vegan"]').remove();
  } else {
    $('option[value="Food"]').remove();
  }
  tags.append(span);
}

function addOption(span) {
  var x = document.getElementById("tag");
  var option = document.createElement("option");
  option.textContent = span.textContent;
  x.add(option);
  span.remove();
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-sm-3">
  <div class="form-group">
    <label class="control-label">Food Type</label><br>
    <span id="tags"></span>

    <select id="tag" onchange="addTag(this)" class="form-control">
      <option value="">-All-</option>
      <option value="Fast Food">Fast Food</option>
      <option value="Vegan">Vegan</option>
      <option value="Food">Food</option>
    </select>
  </div>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get select option by value using javascript

Javascript - How to fire a function using <option>'s value in event listener

How to add <a> tag in dropdown option value

How to add the two textbox value using javascript

How to empty a value from the first option of a select box option using javascript or jquery?

How to add option to javascript RegExp object

How to add text in option using jquery?

How to add a value randomly to an array using JavaScript

How to set option value dynamically using jquery/javascript

Add value in span using JavaScript

How to add a Property Name and Value to a JavaScript Object using a function?

How to add element in javascript with value option selected

How to get value of a custom attibute inside a <option> tag using JAVASCRIPT

How to add a character in value of option

How to add row in table with Javascript , table value checkbox,textbox,radio,select option?

How to add key and value from JSON string into innerHTML using JavaScript

In Javascript how do I add a new option into Html Select so inserted based on alphabetical value of text

how to add serial number selected option value

How to add key and its value in existing json array using javascript

how to retrieve the text of option tag using the value given to it in the option tag using javascript?

How to get the option value in javascript

How to create option list from array using add method in javascript?

How to add an index to each option as a value?

how do I add two values to option value and seperate with with a comma javascript

How to add option to change view type to days using JavaScript Calendar (in my Angular APP)?

How to add a selected attribute in option using button using javascript

How can i get the value of the selected option using Javascript or Jquery?

Setting multiple values for option using javascript (add)

How to add value attribute to option element from array in javascript?