Bootstrap Input Multiple Tags Using Tag Manager: Step-by-Step Guide

19 Oct 2024 01:02 PM

Creating a user-friendly interface for managing multiple tags can significantly enhance your web application’s usability. In this guide, we’ll use the Tag Manager jQuery plugin along with Bootstrap to implement an input field that allows users to add, remove, and display multiple tags seamlessly. Let’s dive into the step-by-step process!

Step 1: Set Up Your HTML Structure

First, we need to create the basic HTML layout that includes Bootstrap styles and the input field for tags.

Example HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Input Multiple Tags Example</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tagmanager/3.0.0/tagmanager.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tagmanager/3.0.0/tagmanager.js"></script>
</head>
<body>

<div class="container mt-5">
    <h2>Input Multiple Tags Example</h2>
    <label for="tags">Add Tags:</label>
    <input type="text" id="tags" class="form-control" placeholder="Add tags...">
</div>

<script>
// jQuery code will go here
</script>

</body>
</html>

Explanation of the HTML Structure

  1. DOCTYPE and HTML Tags: The document starts with the standard <!DOCTYPE html> declaration followed by the opening <html> tag.

  2. Head Section:

    • The <head> includes meta tags for character set and viewport settings.
    • Bootstrap CSS is linked for styling.
    • The Tag Manager CSS file is included for the tag input styling.
    • jQuery and Tag Manager scripts are linked to enable functionality.
  3. Body Section:

    • A Bootstrap container wraps our content, providing a structured layout.
    • An input field for tags is created with the ID tags, where users can add multiple tags.
 

Step 2: Initialize the Tag Manager

Next, we’ll add jQuery code to initialize the Tag Manager on the input field. This will enable the functionality to add and remove tags.

jQuery Code

Insert the following code within the <script> tags:

$(document).ready(function() {
    $('#tags').tagManager();
});

Explanation of the jQuery Code

  • Document Ready Function: The $(document).ready(function() { ... }); ensures that the jQuery code runs only after the DOM is fully loaded.
  • Tag Manager Initialization: The line $('#tags').tagManager(); initializes the Tag Manager on the input field with the ID tags, enabling tag management features.

Step 3: Full Implementation

Here’s the complete code combining the HTML and jQuery:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Input Multiple Tags Example</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/tagmanager/3.0.0/tagmanager.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tagmanager/3.0.0/tagmanager.js"></script>
</head>
<body>

<div class="container mt-5">
    <h2>Input Multiple Tags Example</h2>
    <label for="tags">Add Tags:</label>
    <input type="text" id="tags" class="form-control" placeholder="Add tags...">
</div>

<script>
$(document).ready(function() {
    $('#tags').tagManager();
});
</script>

</body>
</html>

Step 4: Testing the Tag Manager

  1. Open Your HTML File: Save the complete code in an HTML file and open it in a web browser.
  2. Add Tags: Click inside the input field and start typing a tag. Press Enter or Comma (,) to add the tag.
  3. Remove Tags: Click the "X" on any tag to remove it.

Conclusion

Congratulations! You’ve successfully implemented a multiple tags input feature using the Tag Manager jQuery plugin and Bootstrap. This functionality enhances user interaction by allowing easy management of tags, which is especially useful for forms and applications requiring categorization or labeling.

Feel free to customize the styles or functionalities further to fit your project needs. If you have any questions or run into issues, don’t hesitate to ask. Happy coding!

 

 

 

2
30