What is the best way to eliminate products that have already been utilized?

Take a look at my code snippet.

$(function() {

  $("#tags input").on({
    focusout: function() {
      var txt = this.value.replace(/[^a-z0-9\+\-\.\#]/ig, ''); // only allow certain characters
      if (txt) $("<span/>", {
        text: txt.toLowerCase(),
        insertBefore: this
      });
      this.value = "";
    },
    keydown: function(ev) {
      if (/(188|13|32)/.test(ev.which)) { // check for comma, enter or space key press
        $(this).focusout();
      } else if (ev.which === 8 && this.value == '' && $(this).prev("span").hasClass('toRemove')) {
        $(this).prev("span").remove(); // remove previous span tag
      } else if (ev.which === 8 && this.value == '') {
        $(this).prev("span").addClass('toRemove'); // add class to previous span tag
      } else {
        $(this).prevAll('.toRemove').removeClass('toRemove'); // remove class on key down
      }
    }
  });

  $('#tags').on('click', 'span', function() {
    $(this).remove();
  });

});
#tags {
  float: right;
  border: 1px solid #ccc;
  padding: 5px;
  font-family: Arial;
}
/* CSS rules for tags styling */
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- HTML structure for tags -->

I am working on creating an autocomplete feature for the input field. The current suggestions are static, but I want to make them dynamic. I have created an array called tags with various options. My goal is to filter this array based on the user's input and show relevant suggestions dynamically.

var tags = ["HTML", "MySQL", "CSS", "SQL", "Lisp", "jQuery", "PHP", "C", "C#", "JAVA", "android"];

I need to search through the tags array and return items that contain a substring of the user's input. Here is a simplified version of what I'm trying to achieve:

$("#tags input").on('keydown', function(){
    for (i = 0; i < cars.length; i++) { 
        if (tags[i].toLowerCase().indexOf("user's input".toLowerCase()) >= 0) {
            return tags[i];
        }
    }
});

My question is: How can I exclude tags that have already been selected by the user from the autocomplete suggestions list?

For example, if the user has selected the SQL tag, and then types SQ, I want to display only the MySQL tag as a suggestion, not both MySQL and SQL.

Is there a way to achieve this without using jQuery UI?

Answer №1

If you want to adjust your tags object based on the selection, consider restructuring it as shown below:

var tags = [{ tagName : "Java", selected : false},
            { tagName : "Python", selected : false},
            { tagName : "Javascript", selected : false}}
           ]

Update your search function to something similar to the following code snippet...

$("#tags input").on('keydown', function(){
for (i = 0; i < tags.length; i++) { 
    if (tags[i].name.toLowerCase().indexOf("user's entry".toLowerCase()) >= 0 && !tags[i].selected) {
        tags[i].selected = true;
        return tags[i];
    }
}

Hopefully, this solution proves useful for your needs.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

"Fixing the position of a div on the Samsung Galaxy S8 navigation bar to

Here is the HTML code I am working with: <div class="app-bar"> <a href="#'>icon</a> <a href="#'>icon</a> <a href="#'>icon</a> <a href="#'>icon</a> </div>' ...

The issue with linking to files on a custom 404 error page is that it doesn't function properly when

Having some trouble with my 404 page. I seem to have figured out the issue. The following URL works fine: sia.github.io/404.html and so does this one: sia.github.io/ooofdfsdfaablahblah However, when navigating to sia.github.io/1/2/3/, everything seems to ...

Guide on removing material-ui from your project and updating to the newest version of MUI

I need to update my React app's material-ui package to the latest version. Can someone provide instructions on how to uninstall the old version and install the new MUI? UPDATED: In my package.json file, the current dependencies are listed as: ...

Mastering the art of using jQuery AJAX in conjunction with JSP for efficient database updates

I've been working with ajax and jquery to call a jsp page that submits form data to a database. However, I'm encountering an issue where I receive a "Null values can't be inserted to database" exception when running the insert query. Oddly e ...

Sliding horizontally with a responsive touch

I am looking to implement a horizontal responsive page navigation, similar to the illustration shown below: This is my current progress: DEMO $(document).ready(function () { var slideNum = $('.page').length, wrapperWidth = 100 * s ...

CSS Grid with dynamically changing number of rows set to "auto", while ensuring one row always has a fixed size of "1fr"

Currently, I am experimenting with a CSS grid-based frontend and have encountered a specific requirement that keeps repeating itself in various parts of the frontend: A grid with a dynamic number of rows. Each row should be of variable size (auto). The f ...

Combining the functionality of will_paginate with the sleek design of jQuery

Hello there! I am currently working with a jQuery UI modal dialog. Inside this dialog, I have incorporated two separate jQuery UI tabs. Each tab contains a div that displays a list of items which are paginated using will_paginate in Rails. However, I am ...

Customize the select options of the jquery-image-dropdown plugin

I am aiming to dynamically remove the existing options in a select menu and replace them with a single new value. The select menu is constructed using the following plugin: Visit jquery-image-dropdown plugin webpage However, my jquery script seems to be ...

The visibility of JavaScript script to Tomcat is obscured

Working on a web project using servlets and JSP in IntelliJ. One of the JSP files manages graphics and user forms with graphics.js handling graphics and form validation done by validator.js. The issue I'm facing is that while Tomcat can locate graphi ...

Changing the background color of a PHP input based on the webpage being viewed - here's how!

I'm in the process of creating a website where each page will have its own unique background color. Additionally, I am using a PHP input for both the header and footer sections, which need to change their background colors based on the specific webpa ...

What is the method for embedding the creation date of a page within a paragraph on Wagtail?

Whenever a page is created in the admin panel of Wagtail, it automatically displays how much time has elapsed since its creation. https://i.stack.imgur.com/6InSV.png I am looking to include the timestamp of the page's creation within a paragraph in ...

"Utilizing long-polling techniques for cross-subdomain AJAX requests

Currently, I am developing a notifications script that continuously checks a database for any updates and displays them in a custom JavaScript popup. I have successfully implemented the jQuery AJAX loading and processing script as well as the PHP long pol ...

Guide on creating multiple instances of vue-multiselect with a simple button click

I am trying to implement a vue-multiselect dropdown with the addition of a new dropdown upon clicking an "add more" button. However, I am currently unsure of the best approach to achieve this. Problem/Question: When adding 2 dropdowns, if the same option ...

Troubleshooting JavaScript within Embedded Resources

Looking for the most effective method of debugging JavaScript in Visual Studio when dealing with embedded resource files? Due to the fact that JavaScript is compiled into a library, setting breakpoints directly on the source file may not yield desired res ...

Having issues with clicking on a row in the table while using AJAX functionality

Experiencing a puzzling issue while attempting to add click functionality to table rows with AJAX, here is the JavaScript code used: //for tabs $(document).ready(function () { $("#tabs").tabs(); }); $(window).load(function() { jsf.ajax.addOnEven ...

jQuery class does not apply correctly in IE9

Currently, I'm designing a menu for a WordPress theme and facing a styling issue. I aim to differentiate between menu items with submenus and those without by applying different styles on hover. Here's the jQuery code snippet I am using: $(docum ...

Troubleshooting CSS Code Issues on Myspace

Greetings! I am currently working on enhancing a friend's profile on myspace.com. I have two divs on each side of the screen that I want to align next to each other. I have tried using float: left; and float: right;, as well as experimenting with marg ...

Displaying JSON data dynamically by iterating through it in a loop

While working with JSON data in a loop, I noticed that the code quality does not meet my expectations. I have a feeling that I might be missing something in my approach. $(function(){ $.getJSON('data.json', function(data){ let content ...

Does angular-sortablejs work with angular 5?

I attempted to use angular-sortables in combination with the ng-drag-drop library to sort the list items that are being dragged, but it appears that nothing is happening when I try to use it. Does anyone know if angular-sortables is compatible with Angular ...

`Why won't my xpath expression function properly?`

<DOC NUMBER=1> <DOCFULL> --> <br><div class="c0"> <p class="c1"><span class="c2">Document 1 of 3</span></p> </div> <br><div class="c0"> <br><p class="c1"><span class="c2"&g ...