how to append element in datalist in form using javascript

javascript loop through datalist
datalist option selected event javascript
how to hide datalist in javascript
javascript set input datalist
load datalist from javascript
get a data-attribute value from the selected datalist option
remove option from datalist javascript
datalist innerhtml

when I am removing form tag then it is working but after adding form tag from HTML it is not working. following is the code on which I am trying and what is the reason it is not working

function abc() {
  var i, ele, node, parent;
  var num = document.getElementById("name").value;
  //num=parseInt(num);
  var parent = document.getElementById("listName");
  var node = document.createTextNode(num);
  var ele = document.createElement("option");
  ele.append(node);
  parent.appendChild(ele);
  //alert(num);
  //num++;
  document.getElementById("name").value = "";
}
<form>
  <input type="input" id="name" list="listName" />
  <datalist id="listName"></datalist>
  <input type="submit" onclick="abc()" />
</form>

Valuing the attribute type of the input element with the submit value means submit the form.

The button documentation states indeed :

submit: The button submits the form data to the server. This is the default if the attribute is not specified, or if the attribute is dynamically changed to an empty or invalid value.

You don't have any form, so the current page is considered as the actual form. As you click on the button, the function associated to onclick() is first invoked. It adds the option in the dataList but you will never see it because the form is submitted and so you come back to the initial state of the html page.

You don't want submit a form but having a button to bind a click event to a function.

So don't use the submit type but the button type for your input :

  <input type="button" value="add option" onclick="abc()" />

that matches to your requirement :

button: The button has no default behavior. It can have client-side scripts associated with the element's events, which are triggered when the events occur.

As a side note, your function is more complex as required and introduces too many variables that may create side effects. This is enough :

function abc() {  
  var nameElement = document.getElementById("name");   
  var newOptionElement = document.createElement("option");
  newOptionElement.textContent = nameElement.value;

  var listNameElement = document.getElementById("listName"); 
  listNameElement.appendChild(newOptionElement);
  nameElement.value = "";
}
<form>
  <input type="input" id="name" list="listName" />
  <datalist id="listName"></datalist>
  <input type="button" onclick="abc()" />
</form>

Inserting options from an array to a datalist using JavaScript, Adding options to a datalist by JavaScript. the format to store the options in a string variable by using all elements. More on JavaScript form Validation. We will use one array to store months of a year and then add them as option to the datalist. We will loop through all the elements of the array and prepare the format to store the options in a string variable by using all elements.

Because you used button as submit type. If you need client side manipulation then it should not be maintain the page state (means not submit). In your case if you will use

<input type="button" onclick="abc()" />

in place

<input type="submit" onclick="abc()" /> 

so it will be solve your problem.

Salesforce, When I select a value from a html datalist it should get removed from the datalist and This function will remove selected value from datalist and append selected value <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.​js"></script> var html = '<span id='+selectedSector+'><input type=\"checkbox\"​  Create a <p> element with some text and append it to the end of the document body: var x = document.createElement("P"); // Create a <p> node var t = document.createTextNode("This is a paragraph.");

change the html to type=button

<input type="button" onclick="abc()"/>

Salesforce, In a scenario there is a Datalist and there are some Datalist values in that Datalist​. <td><input type="checkbox" onclick="checkboxClick(this)" id="ck_'+idCounter​+'"/></td> As needed, you can use the selected value to display it on another div with add the value into the original datalist by using javascript push() function. The <datalist> tag is used to provide an "autocomplete" feature for <input> elements. Users will see a drop-down list of pre-defined options as they input data. The <datalist> element's id attribute must be equal to the <input> element's list attribute (this binds them together).

this works for me : html ->

<input type="text"  class='form-control' list="city" >
<datalist  id="city">
</datalist>

js and jq ->

$("#city").empty(); // first empty datalist 
var options=[];
options[0] = new Option('landan');
options[1] = new Option('york');
options[2] = new Option('liverPool');
$("#city").append(options);

HTML DOM Datalist options Collection, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  The HTML <datalist> element contains a set of <option> elements that represent the permissible or recommended options available to choose from within other controls. The source for this interactive example is stored in a GitHub repository.

HTML DOM appendChild() Method, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  Datalist Object. The Datalist object represents an HTML <datalist> element. Note: The <datalist> element is not supported in Internet Explorer 9 (and earlier versions), or Safari. Access a Datalist Object. You can access a <datalist> element by using getElementById():

HTML DOM Datalist Object, Example. The following code shows how to get access a <datalist> element by using getElementById() <!DOCTYPE html> <html> <body> <form> <input list="​myDatalist"> <datalist id="myDatalist"> <option appendChild(x); var y = document. Creating New HTML Elements (Nodes) To add a new element to the HTML DOM, you must create the element (element node) first, and then append it to an existing element.

HTML DOM Datalist Object, The input box is then appended to our form using the appendChild() method and supplying it “FORM1” as parameter value. We then create the <  A datalist element can be placed anywhere in the markup, but you associate the target input element by adding a list attribute. The list attribute's value is the 'id' of the related datalist. Once this relationship is established the user will have access to the suggestion list.

Comments
  • Use type="button" instead of type="submit"
  • But what if I want to use input type 'submit', then what changes I have to do for that.
  • You should persist the submitted data. And you should populate the datalist with these persisted data.
  • sorry! It is not working with onsubmit and what is the reason for it?
  • you dont need onsubmit you can only change to <input type="button" onclick="abc()"/>