0

I have to add and remove an input field in div with id "add", So the following code is not working.

<div id="add">
</div>

<button id="add-el">Add</button>
<button id="add-el">Remove</button>

$("#add-el").on("click", funtion () {

   var input = $("<input class=\"some-class\" type=\"email\" name=\"email\" 
    required=\"\" placeholder=\"Enter your address\" autocomplete=\"off\">");
   $("#add").append(input);

});

$("#remove-el").on("click", funtion () {

   $("#add").remove(input);

});
1
  • you cannot access the input variable inside the click function of the #remove-el. input is a local variable of the #add-el click function. Commented Jul 20, 2018 at 7:07

3 Answers 3

1

Create element using jQuery(html, attributes) instead of string concatenation.

You can use .empty() to clear the <div> content. Or, define input variable outside event handler and use it to remove the element.

var input;
$("#add-el").on("click", function() {
  input = $('<input />', {
    "class": "some-class",
    "type": "email",
    "placeholder": "Enter your address"
  });

  $("#add").append(input);
});

$("#remove-el").on("click", function() {
  //$("#add").empty();

  if (input && input.length) {
    input.remove();
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="add">
</div>

<button id="add-el">Add</button>
<button id="remove-el">Remove</button>

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

4 Comments

He also has other problem (in remove) with the input-variable not being global, maybe add that also here, was about to answer :)
Satpal - what is OP here?
@Aditya, You are OP(Original Poster)
Thank you satpal, will upvote and accept your answer once checked
0

There are multiple problems in your code.

You have a SyntaxError because you have a new line before required=\"\" and you write funtion instead of function

var input

$("#add-el").on("click", function() {

  input = $("<input class=\"some-class\" type=\"email\" name=\"email\" required = \"\" placeholder=\"Enter your address\" autocomplete=\"off\">");
  $("#add").append(input);

});

$("#remove-el").on("click", function() {
  input.remove();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="add">
</div>

<button id="add-el">Add</button>
<button id="remove-el">Remove</button>

And IDs have to be unique, you must not have to elements with the id add-el.

And you cannot access the input you defined in the add-el callback in the remove-el callback.

Comments

0

There is issue in your code: https://codepen.io/creativedev/pen/pZNOwq

Change funtion to function and it will work. See demo

$("#add-el").on("click", function () {

   var input = $("<input class=\"some-class\" type=\"email\" name=\"email\" required=\"\" placeholder=\"Enter your address\" autocomplete=\"off\">");
   $("#add").append(input);

});

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.