Extracting names from HTML can be done easily by looking for the "@" symbol that the

My HTML structure looks like this:

<table id="table-15244" cellspacing="0" class="comment"><tbody>
    <tr id="comment-37">
        <td style="color: #999" class="NV">
        </td>
        <td class="hidden-mob">
            <a><i class="fa fa-caret-up c-voteup"></i></a><br>
            <a><i class="fa fa-bug c-bug"></i></a>
        </td>
        <td class="CT">This is first comment
            <samp>ــ</samp>
            <span>
                <a>Jack</a>                                      // Here
            </span>
            <span class="date_time">
                <span></span>
            </span>
        </td>
    </tr>

    <tr id="comment-37">
        <td style="color: #999" class="NV">
        </td>
        <td class="hidden-mob">
            <a><i class="fa fa-caret-up c-voteup"></i></a><br>
            <a><i class="fa fa-bug c-bug"></i></a>
        </td>
        <td class="CT">This is second comment
            <samp>ــ</samp>
            <span>
                <a>Peter</a>                                      // Here
            </span>
            <span class="date_time">
                <span></span>
            </span>
        </td>
    </tr>
</tbody></table>

I want to extract two names from the HTML and display them as a pop-up after entering @ followed by their initial characters (in this case, J, P). The pop-up should be hidden if the entered text does not match these names, similar to stackoverflow's comments textarea.

How can I achieve this?

HTML:

// the above HTML is here
<textarea class="comment" rows="4" cols="50"></textarea>

JS:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>

$(".comment").on('keydown', function(e){
    if (e.which == 50) {  // @ pressed

        /* wait for first character that will be entered
         * if it is match with one or more of those names in the html
         * then show that name as a pop-up on the top of textarea
        */
    }
});

Answer №1

Include a specific class in all elements containing names (or any other element with a name) such as Peter

Then, utilize jQuery to select and retrieve all the names in your JavaScript.

name_elements = $('.name');
names_list = [];
for (var i = 0; i < name_elements.length;i++){names_list.push(name_elements[i].text)}

Now you will have an array of names readily available for use in your keydown handler.

In addition, to display matching names as you type, consider using a library like https://jqueryui.com/autocomplete/ (or explore other options available online)

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

What's causing the unexpected rendering outcome in Three.js?

Here is a mesh created in Blender: https://i.sstatic.net/KBGM5.jpg Upon loading it into Three.js, I am seeing this result: https://i.sstatic.net/PCNQ8.jpg I have exported it to .obj format and ensured all faces are triangulated. I am not sure why this is ...

Is it possible to turn off the fallback color for bourbon radial gradients?

Is there a way to prevent the fallback from being applied when using the radial gradient mixin in Bourbon? I've consulted the documentation at , which states that the fallback is optional. However, no matter what I attempt, it seems that the primary ...

Is it necessary to execute 26 individual mysql queries in order to group items alphabetically in a directory listing page?

I am looking to create a directory listing page for my website that displays all of my articles grouped by the first letter of their titles. Do I have to execute 26 MySQL queries like: mysql query like "SELECT * FROM articles Where title like 'a%&ap ...

Angular's UI Modal: utilizing inline template and controller functionality

I am looking to create a simple confirmation box using UI-modal, which I have used successfully for more complex modals in the past that load their template and controller from external files. However, this time I want something even simpler - just a basi ...

Utilizing an ajax request to invoke a PHP function rather than using a traditional

Within my page, I have included a file named functions.js. In this file, there is a line that reads: ajaxRequest.open("GET", "save.php?json=" + jsonstring + "&rand=" + rand, true); How can I invoke a php method using ajaxRequest? Additionally, I woul ...

Is it possible to restrict the application of jquery .css() to a particular media query only?

Whenever a user's browser width is below 1160px, a media query is set up to hide my website's right sidebar. They can bring it back by clicking on an arrow icon, causing it to overlap the content with absolute positioning. To achieve this functi ...

Exploring the Features of Bottom and Right in jQuery

Here is a snippet of jQuery code that I included in a sample program: $('#left').click(function(){ $('.box').animate({ left: "-=40px", }, function(){/* End of Animation*/}); }); $('#right ...

How can you determine whether the CSS of a <div> is defined in a class or an id using JavaScript/jQuery?

Hey there, I'm currently working on dynamically changing the CSS of a website. Let's say we have a div like this: <div id="fooid" class="fooclass" ></div> The div has a class "fooclass" and an ID "fooid", and we're getting its ...

Navigate to the parent element in the DOM

Looking to add some unique styling to just one of the many Mat dialog components in my project. It seems like modifying the parent element based on the child is trickier than expected, with attempts to access the DOM through the <mat-dialog-container> ...

Transfer data from a file to a PHP file using the XMLHttpRequest object in JavaScript and receive

I'm attempting to use AJAX to send an image file to a PHP file. The issue is that even though the script sends the object, my parser isn't able to retrieve the $_FILES["avatar"]["name"] and tmp_name values. Is there a method to transfer the file ...

Using an image within the input group in Bootstrap 4

I'm a beginner in web development and I'm currently utilizing the glyphicon. This is how I'm currently using it: const className = `form-group ${touched && error ? 'has-danger' : ''}`; <div className={classN ...

Tips for preserving the contents of a list with HTML and Javascript

My latest project involves creating a website with a To-Do list feature. Users should be able to add and delete items from the list, which I achieved using JavaScript. Now, my next goal is to ensure that the current items on the list are saved when the pag ...

Tips for expanding the contentedible HTML element downwards within its container

When a user inputs content, I need the paragraph to expand only downward while the div with the ID of description also grows in the same direction. The main parent div labeled documentation should remain fixed so it doesn't expand upwards. This scenar ...

What steps should I take to solve the issue of a black screen showing up once my React website has finished loading

Here's a photo showing error messages displayed on my website. Strangely, there are no errors appearing in my terminal and the website loads perfectly fine. However, I encountered some issues when trying to make styling changes using my dev tools. Aft ...

Organize JSON objects by their keys using jQuery's .each() method

{ "staff": { "2010": [ { "first_name": "john", "last_name": "doe", }, { "first_name": "jane", "last_name": "doe", } ], "2012": [ ...

The AJAX request to the GitHub OAuth API using jQuery encountered an unknown issue and returned an unexpected error

Let me start by sharing the following code snippet: var requestAuthUri = 'https://github.com/login/oauth/access_token'; var ajaxArgs = { type : 'POST', url : requestAuthUri, headers : { &ap ...

Is it possible to design and implement Materialize CSS inputs without relying on Materialize CSS?'

Is there a method to implement Materialize CSS input elements that include placeholders and icons without directly using the Materialize CSS framework? I prefer not to mix frameworks in order to avoid potential conflicts, as I am already utilizing anothe ...

Changing the time zone of a browser using JavaScript or jQuery

After researching numerous discussions on the topic, it appears that changing the browser's timezone programmatically is not possible. Although the moment-timezone.js library allows us to set timezone for its objects, it does not affect the browser&ap ...

How do I directly display a variable in index.html using node.js?

Is there a way to retrieve user inputs from the index.html file, process them in Node.js, and then display the result back on the index.html page instead of redirecting to a new page? Form Handling with Express var express = require('express'); ...

Update the header by changing the background of all elements within it, while keeping the rest of the page as is

I am currently creating a Gatsby site with a layout and header component to construct pages. Each component has its own stylesheet (layout.module.scss and header.module.scss). Below is the code snippet: /* layout.js */ import React from "react" ...