How to use JavaScript to implement the label input box function?
How to use JavaScript to implement the tag input box function
The tag input box is a common user interaction component that allows users to enter multiple tags and can be added dynamically , delete the label. In this article, we will use JavaScript to implement a simple label input box function. The following is a specific implementation code example:
HTML structure
First, we need to create an <input>
element for the input tag and a display in HTML The <div>
container of the tag. The HTML code is as follows:
<input id="tag-input" type="text" placeholder="请输入标签"/> <div id="tag-container"></div>
JavaScript code
Next, we use JavaScript to implement the function of the label input box. The code is as follows:
// 获取标签输入框和标签容器元素 const input = document.getElementById('tag-input'); const container = document.getElementById('tag-container'); // 定义一个数组来存储用户输入的标签 let tags = []; // 监听键盘按下事件,回车键添加标签 input.addEventListener('keydown', function(event) { if (event.keyCode === 13) { const tag = input.value.trim(); // 检查标签是否已存在 if (tags.includes(tag)) { alert('该标签已存在,请输入其他标签'); } else if (tag !== '') { // 添加标签到数组 tags.push(tag); // 清空输入框并重新渲染标签 input.value = ''; renderTags(); } } }); // 渲染标签函数 function renderTags() { // 清空标签容器 container.innerHTML = ''; // 遍历标签数组,创建标签元素并加入容器 tags.forEach(function(tag) { const tagDiv = document.createElement('div'); tagDiv.innerText = tag; // 为每个标签添加一个删除按钮 const deleteButton = document.createElement('span'); deleteButton.innerText = 'x'; deleteButton.addEventListener('click', function(event) { // 获取被点击的标签的索引 const index = tags.indexOf(tag); // 从标签数组中删除该元素并重新渲染标签 tags.splice(index, 1); renderTags(); }); tagDiv.appendChild(deleteButton); container.appendChild(tagDiv); }); }
Code analysis
The above sample code implements the function of the label input box through the following steps:
- Get the label input box and label The container's DOM element.
- Define an empty array
tags
to store the tags entered by the user. - Listen to the keyboard press event. When the user presses the Enter key, add the value of the input box as a new tag to the
tags
array, and re-render the tag. - Definition
renderTags
Function to render tags. It first clears the tags container, then iterates through thetags
array, creates tag elements and delete buttons, and adds them to the container. - The click event listener for the delete button allows the user to delete the label. It finds the clicked tag's position in the
tags
array, removes the element, and re-renders the tag.
Above, we have successfully used JavaScript to implement a label input box with add and delete functions. You can copy the above code into your project and use it with other files to implement more complex functions. Hope this article helps you!
The above is the detailed content of How to use JavaScript to implement the label input box function?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

This tutorial shows you how to find specific text or phrases on all open tabs in Chrome or Edge on Windows. Is there a way to do a text search on all open tabs in Chrome? Yes, you can use a free external web extension in Chrome to perform text searches on all open tabs without having to switch tabs manually. Some extensions like TabSearch and Ctrl-FPlus can help you achieve this easily. How to search text across all tabs in Google Chrome? Ctrl-FPlus is a free extension that makes it easy for users to search for a specific word, phrase or text across all tabs of their browser window. This expansion

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

As a popular short video social platform, Douyin has a huge user base. For Douyin creators, using tags to attract traffic is an effective way to increase the exposure of content and attract attention. So, how does Douyin use tags to attract traffic? This article will answer this question in detail for you and introduce related techniques. 1. How to add tags on Douyin to attract traffic? When posting a video, make sure to choose tags that are relevant to the content. These tags should cover the topic and keywords of your video to make it easier for users to find your video through tags. Leveraging popular hashtags is an effective way to increase your video’s exposure. Research current popular tags and trends and incorporate them into your video descriptions and tags. These popular tags usually have higher visibility and can attract the attention of more viewers. 3. Label
