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

Node.js server for Cross-Origin Resource Sharing

I'm brand new to Node.js and I'm trying to run some example code, but I keep encountering CORS issues. Server.js var http = require('http'); var io = require('socket.io'); server = http.createServer(function(req, r ...

What is the reason for the presence of additional space below when using x-overflow:hidden?

When I place two spans inside each other and use overflow-x:hidden on the inner span, it results in extra space below the inner span. Why does this happen? <span style="" class="yavbc-color-tip"><span style="">Some text</span></span&g ...

template not being loaded with form

My Django form is not showing up when I try to implement it. Even after checking the browser's inspector, there is no sign of it being loaded at all. If more information is required, please let me know. Here is the project structure: /beerad url ...

Execute a function upon the invocation of a different function

I am exploring how to trigger a function when another function is executed. While addEventListener is typically used for events like "click" or "mouseover", I am looking to detect when a function is called. For instance: Function 1 is invoked, an ...

extracting data from selected checkboxes using ajax

I am having an issue trying to retrieve the values of the checked checkboxes here. The problem arises because it returns as an array and ends up being undefined. Can someone please assist me with a solution? Thank you. <form> <input type="check ...

Unable to insert values into database using PHP

I've put together a user registration form using PHP, jQuery, and SQL. I have everything set up to send the details to the database via an AJAX request. The code is running smoothly without errors, but for some reason, the values are not being added t ...

Signs that indicate a socket has truly disconnected

socket.on('disconnect') isn't behaving as I anticipated. I want to be able to track when a user leaves my website completely, not just switches pages. Currently, navigating to a different route on my webpage triggers the socket disconnect ev ...

Ways to eliminate all characters preceding a certain character within an array dataset

I am currently working on parsing a comma-separated string retrieved from a web service in my application, which contains a list of user roles. My goal is to convert this string into an array using jQuery, and I have successfully achieved that. However, I ...

Exploring the use of radio buttons with Bootstrap and AngularJS

Having a radio button like this: <tr> <td>GTS ? </td> <td> <div class="col-md-10 columns"> <ul id="GTSD" class="radio" role="menu" aria-labelledby="menu1"> <label class= ...

The issue of Ajax response not triggering the ng-click function

When attempting to pass data through an AJAX return, I encountered an issue with a function that writes an ng-click in Angular 1. The code snippet is as follows: $.ajax({ 'method': 'GET', 'url': base_url +&apos ...

Guide to invoking a jQuery function by clicking on each link tab

Below is a snippet of jQuery code that I am working with: <script> var init = function() { // Resize the canvases) for (i = 1; i <= 9; i++) { var s = "snowfall" + i var canvas = document.getElementById( ...

Having trouble getting the form to serialize properly, unable to locate the error. Frustrating!

Hello, Here is the jQuery code I am currently using: $('body').on('change','form#item-form', function(e){ e.preventDefault(); var data = $(this).serialize(); console.log( data ); }); and a form (inserted via que ...

Display a division upon clicking a button using AngularJS and HTML

Looking to implement a functionality in my HTML page using angularJS. I am currently learning angularJs and experimenting with a feature where clicking on a button will display a specific div (EmployeeInfoDiv) on the webpage. The idea is to fill out some t ...

Adjust the size of col-lg-3

Is there a way to adjust the size of my col-lg based on different screen resolutions? I'm looking for a solution that can change the size of col-lg depending on the screen resolution. For example: .col-lg-3 { /* styles for screens with 1366x768p ...

Tips on sending error messages to an MVC view during an Ajax call

When using ajax to call a controller action method on an MVC button click event, there may be validation logic in the controller that needs to notify the user of any errors. Is there a way to send an error message to the ajax success event from the control ...

Django: Sending Templates through AJAX for Dynamic Rendering

Hey there, this is more of a theoretical question. So I recently discovered a cool trick: using AJAX to trigger a function that delivers a pre-rendered HTML template as a response, which can then be applied to a designated div element. This method feels i ...

Having trouble with Window.open on Mobile Devices and Canvas?

I've created a unique "button" within the div element. This button is designed to detect user interaction, such as clicks or taps, and when triggered, it should open a new window with the URL: "http://www.google.com". However, while this functionality ...

CSS - Adding a Distinctive "Line" to a Navigation Bar

I am attempting to design a unique navbar layout with two "lines". The first line should consist of several links, while the second line would include an HTML select field with a submit button. The following code will generate the desired navbar visual ou ...

When trying to locate an item in an array within a VUE application, it may result in

I've got this code snippet that successfully finds the item details in an array: var findGroupId = medias.find(medias => medias.group_name === this.groupName) The medias variable is an array. When I run console.log(findGroupId), I get this result ...

Is it possible for a single PHP query to manage multiple AJAX requests from various pages simultaneously?

My PHP page is called update_details.php?id=xyz. This page has a query that gets user details and updates their login time. Each user has a unique profile page named profile.php?id=xyz. So, the profile pages are different for each user such as profile.php ...