How do I remove all tags in an input field below with jQuery?
Asked Answered
C

6

8

How do I remove all tags in an input field below with jQuery?

<input class="form-control" id="videotags" data-role="tagsinput"  placeholder="Video Tags" type="text">

  //what I tried:
<script>$("#videotags").remove();</script>
Chuckhole answered 18/3, 2017 at 15:14 Comment(1)
Do you want to clear input value or you want to remove input element from the dom ?Admissive
B
23

If you are using Bootstrap tagsinput try this:

$("#videotags").tagsinput('removeAll');

source

Bulwark answered 1/6, 2017 at 13:2 Comment(0)
R
1

This work for me:

$('#videotags').tagsinput('removeAll');
Reverence answered 17/11, 2020 at 13:14 Comment(0)
P
0

Try this

$('#videotags').click(function() {
    var input = $(this);

    input.val('');
});

https://jsfiddle.net/joe_hart/7xrg242b/

Pointless answered 18/3, 2017 at 15:21 Comment(0)
A
0
$("#videotags").replaceWith(function () {
return $('<' + this.nodeName +                   '>').append($(this).contents());
});
Aerify answered 18/3, 2017 at 15:22 Comment(0)
V
0

Are the targets of removing not tags but attributes? The input tag in your question does not include tags.

If you want to remove all attributes in the input tags, I think you can use the method introduced in the below question.

Remove all attributes

Viceregent answered 18/3, 2017 at 15:43 Comment(0)
P
0

bootstrap-tags.js do not provide to delete All tags at once. I have tried some workarounds for this. First I have fetch all the tags. Below is the code for that.

var tagData = $("#small").tags().tagData;
if (tagData.length > 0)
{
    $.each( tagData, function( index, value ){
      for(var tItem=0 ; tItem < tagData.length;)
      {
        $("#small").tags().removeTag(tagData[tItem]);
      }
     });
}

I have used 2 loops here as once you delete one record it will be reindex to 0.

Puzzle answered 28/9, 2017 at 8:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.