I am using bootstrap 5 and I am adding via jquery Pill badges.
When clicking the pill badge I would like to remove it.
Find below my working example:
$("#listBtn").click(function() {
let txt = $('#fruits').val();
$("#symbolPicks").append(`<span class="badge rounded-pill bg-secondary">${txt}</span>`);
$("#fruits").val("");
});
var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substringRegex;
// an array that will be populated with substring matches
matches = [];
// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');
// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
matches.push(str);
}
});
cb(matches);
};
};
var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];
$('#the-basics .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
}, {
name: 'states',
source: substringMatcher(states)
});
span.twitter-typeahead .tt-menu {
cursor: pointer;
}
.dropdown-menu,
span.twitter-typeahead .tt-menu {
position: absolute;
top: 100%;
left: 0;
z-index: 1000;
display: none;
float: left;
min-width: 160px;
padding: 5px 0;
margin: 2px 0 0;
font-size: 1rem;
color: #373a3c;
text-align: left;
list-style: none;
background-color: #fff;
background-clip: padding-box;
border: 1px solid rgba(0, 0, 0, 0.15);
border-radius: 0.25rem;
}
span.twitter-typeahead .tt-suggestion {
/* display: block; */
width: 100%;
padding: 3px 20px;
clear: both;
font-weight: normal;
line-height: 1.5;
color: #373a3c;
text-align: inherit;
white-space: nowrap;
background: #fff;
border: 0;
}
span.twitter-typeahead .tt-suggestion:focus,
.dropdown-item:hover,
span.twitter-typeahead .tt-suggestion:hover {
color: #2b2d2f;
text-decoration: none;
background-color: #f5f5f5;
}
span.twitter-typeahead .active.tt-suggestion,
span.twitter-typeahead .tt-suggestion.tt-cursor,
span.twitter-typeahead .active.tt-suggestion:focus,
span.twitter-typeahead .tt-suggestion.tt-cursor:focus,
span.twitter-typeahead .active.tt-suggestion:hover,
span.twitter-typeahead .tt-suggestion.tt-cursor:hover {
color: #fff;
text-decoration: none;
background-color: #0275d8;
outline: 0;
}
span.twitter-typeahead .disabled.tt-suggestion,
span.twitter-typeahead .disabled.tt-suggestion:focus,
span.twitter-typeahead .disabled.tt-suggestion:hover {
color: #818a91;
}
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.4.0/font/bootstrap-icons.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/js/bootstrap.bundle.min.js" integrity="sha384-b5kHyXgcpbZJO/tY9Ul7kGkf1S0CWuKcCD38l8YkeH8z8QjE0GmW1gYU5S9FOnJ0" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.11.1/typeahead.bundle.min.js" integrity="sha512-qOBWNAMfkz+vXXgbh0Wz7qYSLZp6c14R0bZeVX2TdQxWpuKr6yHjBIM69fcF8Ve4GUX6B6AKRQJqiiAmwvmUmQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script src="https://min.gitcdn.link/repo/Codekutu/Bootstrap4TagsInputWithTypeahead/master/js/bootstrap-tagsinput.js"></script>
<!-- Custom styles for this template -->
<link href="https://unpkg.com/bootstrap-table@1.18.3/dist/bootstrap-table.min.css" rel="stylesheet">
<main class="container pt-3">
<div class="row mt-3">
<form name="watchlistForm" id="watchlistForm" method="POST" action="">
<div id="the-basics">
<div class="input-group">
<input class="form-control" name="name" id="name" type="text" class="form-control me-2" placeholder="Add your Name">
</div>
<div class="input-group">
<input class="form-control typeahead" name="fruits" id="fruits" type="text" class="form-control me-2" placeholder="Pick the symbols that you want to monitor f.ex. AAPL, MSFT, TSLA">
<button class="btn btn-warning" type="button" id="listBtn">Add to List</button>
</div>
<div class="col-sm-3" id="symbolPicks">
</div>
<div class="input-group">
<button type="submit" class="btn btn-warning">Subscribe to symbols</button>
</div>
</div>
</form>
</div>
</main>
When clicking the button I am adding the input value to the list.
I would like to delete the tag if I click it. One further question: Is there also a way in bootstrap 5 to add a X on the top right?
I really appreciate your replies!
