arcticlisa
arcticlisa

Reputation: 73

Uncaught TypeError: $(...).tagsinput is not a function

I have a search box and several check-boxes. Once checked, the value from a checkbox is pushed into the search box.

Now I would like to use Bootstrap Tags Input for each string that is pushed into the search box to look like a tag, but I get the error message in the title.

Am I using Tags Input wrong?

$(document).ready(function () {
        $checks = $(":checkbox");
        $checks.on('change', function () {
            var ingredient = $checks.filter(":checked").map(function (i, v) {
                return this.id;
            }).get().join(" ");
            $('#SearchString').tagsinput(ingredient);
        }).trigger('change');
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<link href="https://cdn.jsdelivr.net/bootstrap.tagsinput/0.8.0/bootstrap-tagsinput.css" rel="stylesheet" />
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://code.jquery.com/jquery-3.2.1.js"></script>
    <script src="https://cdn.jsdelivr.net/bootstrap.tagsinput/0.8.0/bootstrap-tagsinput.min.js"></script>
    
    
   <input class="form-control" id="SearchString" data-role="tagsinput" name="SearchString" type="text" value="">

Upvotes: 3

Views: 2709

Answers (1)

beaver
beaver

Reputation: 17647

Here is your modified snippet:

$(document).ready(function () {
      $checks = $(":checkbox");
      $checks.on('change', function () {
          $('#SearchString').tagsinput('removeAll');
          $checks.filter(":checked").each(function( index ) {
              $('#SearchString').tagsinput('add', this.value);
          });
     }).trigger('change');
});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<link href="https://cdn.jsdelivr.net/bootstrap.tagsinput/0.8.0/bootstrap-tagsinput.css" rel="stylesheet" />
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
    <script src="https://code.jquery.com/jquery-3.2.1.js"></script>
    <script src="https://cdn.jsdelivr.net/bootstrap.tagsinput/0.8.0/bootstrap-tagsinput.min.js"></script>
    
    
   <input class="form-control" id="SearchString" data-role="tagsinput" name="SearchString" type="text" value="">

<div class="checkbox">
  <label><input id="chk1" type="checkbox" value="ingredient1">ingredient 1</label>
</div>
<div class="checkbox">
  <label><input id="chk2" type="checkbox" value="ingredient2">ingredient 2</label>
</div>
<div class="checkbox disabled">
  <label><input id="chk3" type="checkbox" value="ingredient3">ingredient 3</label>
</div>

An improved version, using objects instead of plain texts as tags, is available here: https://jsfiddle.net/beaver71/v44mnn31/16/

In this example when removing a tag in the tagsinput then it's unselected in the relating checkbox.

Upvotes: 1

Related Questions