Discovering the identification of a comment in JavaScript

Here is the code snippet I am working with:

<a onclick="lel($(this),'212345555')" class="button">
<a onclick="lel($(this),'241214370')" class="button">
<a onclick="lel($(this),'248916550')" class="button">
<a onclick="lel($(this),'253234444')" class="button">
<a onclick="lel($(this),'248914570')" class="button">

I am trying to execute all instances of lel($(this),'id') in JavaScript at once, but I am unsure how to retrieve the id for each button.

Answer №1

If you have the skill to modify server-side code in order to achieve the following:

<a id="212345555" class="button">
<a id="241214370" class="button">
<a id="248916550" class="button">
<a id="253234444" class="button">
<a id="248914570" class="button">

and also utilize jQuery like this

$('a.button').click(function(){
  let($(this),this.id);
});

Answer №2

When presented with the following HTML

<a onclick="lel($(this),'212345555')" class="button">
<a onclick="lel($(this),'241214370')" class="button">
<a onclick="lel($(this),'248916550')" class="button">
<a onclick="lel($(this),'253234444')" class="button">
<a onclick="lel($(this),'248914570')" class="button">

You have the option to extract and execute operations on these identifiers using regular expressions, with the help of the JavaScript code below

function gatherAndExecute() {
    // Loop through each 'a.button' element that has an onclick attribute defined
    $('a.button[onclick]').each(function(i, e) {
        // Retrieve the value of the onclick attribute
        var rawId = e.attributes['onclick'].value;
        // Parse the identifier from the attribute
        var id = rawId.match(/lel\(\$\(this\),'(\d+)'\)/)[1];
        // Call the lel function
        lel($(e), id);
    });
}

You can also refer to this working example in a demo: https://jsfiddle.net/k9376p5c/

If possible, consider adding an id attribute to these elements while generating the HTML from your server to avoid the need for parsing identifiers.

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

Remove a function tied to an element on a page that has loaded

I have been struggling to remove a function that is attached to an event using both unbind and off, but unfortunately, it has been unsuccessful. To illustrate this issue clearly, I have created a replica on jsFiddle here. The recreated problem closely res ...

Guide on transferring data from an API using mapped React hooks

I am working with react hooks and attempting to pass a value to the delete function and execute the put function <Fab aria-label="delete" className={classes.fab} value={vacation.id} ...

Apply CSS styling (or class) to each element of a React array of objects within a Component

One issue I'm facing involves adding specific properties to every object in an array based on another value within that same object. One such property is the background color. To illustrate, consider an array of objects: let myObj = { name: "myO ...

Is it possible to optimize the performance of my React and TypeScript project with the help of webpack?

I am working on a massive project that takes 6 to 8 minutes to load when I run npm start. Is there a way to speed up the loading process by first displaying the sign-in page and then loading everything else? ...

Tips for passing parameters in a BootstrapDialog.show({ }) function popup

I am trying to display a popup using BootstrapDialog and pass a parameter with it. I have used the data attribute in my code snippet as shown below: BootstrapDialog.show({ closable: false, title: "This is my custom popup", message: $(' ...

Discover the magic of retrieving element background images on click using jQuery

I am attempting to extract the value for style, as well as the values inside this tag for background-image. Here is the script I have tried: function getImageUrl(id){ var imageUrl = jQuery("."+id+". cycle-slide").attr("src"); alert('' + ima ...

Unlocking the power of variables in Next.js inline sass styles

Is there a way to utilize SASS variables in inline styles? export default function (): JSX.Element { return ( <MainLayout title={title} robots={false}> <nav> <a href="href">Title</a> ...

Can we separate city, state, and country data into individual input fields using Autocomplete and Geonames?

Currently, I have a single INPUT field set up to trigger the jQuery UI Autocomplete function, which pulls data from Geonames API. $( "#city_state_country" ).autocomplete({ source: function( request, response ) { $.ajax({ url: "http ...

``There seems to be an issue with Firefox's background position percentage

After combining multiple images into one to reduce HTTP requests, I utilized background-position in percentage values to control which image was displayed. Since the width of the images is dictated by the parent element, using percentages rather than pixel ...

Dynamic content alongside a stationary sidebar that adjusts in width

Attempting to create a switchable sidebar (toggling between short and long form) using bootstrap, multiple examples, and online resources. The width of the sidebar changes when toggled, and I want the content to appear next to it regardless of its length. ...

Struggling with pagination problems in Bootstrap 4 and looking to integrate QR code functionality into your blade template?

I am currently facing an issue with generating a batch of QR codes using the SimpleQR code generator in Laravel blade template on the fly for printing. The problem arises when a page break occurs, as it breaks the QR code. I attempted to use the break-insi ...

The optimal method to showcase lists of names: HTML or CSS?

I have a roster of 40 individuals, complete with their titles and additional details akin to a yearbook layout without the actual photos. Previously, I relied on HTML tables to format this information, but I am contemplating whether there might be a more e ...

Set up a cronjob based on the specified time entered in an HTML form

For my HTML form, users enter a time in HH:mm format. I aim to set up a cronjob on the system that will automatically delete a specific file based on the user-provided time. The file itself always remains constant, with only the deletion time varying. Th ...

Creating new form fields dynamically using JavaScript (triggered by onClick event)

I want to dynamically add fields based on user interaction. For instance, when the checkbox or radio button is clicked, additional fields like buttons and textfields should appear. Is it possible to achieve this using onClick? If so, can you please provide ...

Troubleshooting problem with Ajax Calendar Extender

I have a text box where I need to save the chosen date from a calendar. To achieve this, I am using an AJAX calendar extender and setting the target control property to the textbox ID. However, when I click on a button on the same page, I lose the selected ...

In the event of any unexpected errors while utilizing an API, a ticket will be automatically generated on Jira through the use of Vue.js

After successfully hitting the API through Postman and creating a ticket on Jira, I encountered a CORS error when attempting to do the same using an index.js file in VueJS. The flow is unclear to me as generating a demo error results in nothing being sho ...

What methods can be used to control the URL and back button in a web application designed similar to an inbox in Gmail?

Similar Question: Exploring AJAX Techniques in Github's Source Browser In my application, there are essentially 2 main pages: The main page displays a list of applications (similar to an inbox) The single application page is packed with various ...

Creating an HTML list that displays the UL Level numbers as the list values can be done by following these steps

Trying to create a styled HTML list with CSS that resembles the following: 1. List item     2. List item         3. List item         3. List item         3. List item   &nbs ...

The intricate process of parsing selectors in jQuery

Information from the document: The $(selector, context) function internally utilizes the functionality of the .find() method. However, when we specify a selector like; $('#a .b .c') or $('#a > .b > .c') and so on. Does this ...

Is coffeescript supported by Moovweb?

Lately, I've been researching the Moovweb platform and I'm curious about its compatibility with CoffeeScript. Could someone share a code example to demonstrate how Moovweb works with CoffeeScript? ...