2

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>

1
  • Use type="button" instead of type="submit" Commented Aug 25, 2017 at 10:24

4 Answers 4

1

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>

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

2 Comments

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.
0

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.

Comments

0

change the html to type=button

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

2 Comments

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()"/>
0

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);

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.