Find the IDs of all input boxes that have a specific class attached

Is there a way to retrieve the ID of an input field that has a specific class attribute?

For example:

<input id="first" class="failed" />
<input id="last" class="failed" />
<input id="city" class="failed" />
<input id="state" class="failed" />

I am looking for a method to display all IDs that have the class "failed" after clicking a button or executing a command.

Answer №1

Notify me of any ID that has the failed class after pressing a button or executing a command.

Utilize the class selector to target elements with a specific class. You can loop through all elements using each(). Attach a click event to the button, and use the button's ID as the selector to bind the event.

View Live Demo

HTML

<input id="first" class="failed" />
<input id="last" class="failed" />
<input id="city" class="failed" />
<input id="state" class="failed" />
<input type="button" id="btn" value="click" />

Javascript

$('#btn').click(function () {
    $('.failed').each(function () {
        alert(this.id);
    });
});

Answer №2

Utilize the .map() method in order to retrieve an array of ids

var ids = $('.failed').map(function(){
    return this.id
}).get();

console.log(ids);
alert(ids)

Answer №3

employ jQuery for every instance

$('.failed').each(function(index){
    var id=$(this).attr('id');//retrieve each individual ID 
  });

Answer №4

Using jQuery's each function to loop through elements with the class "failed" and creating an alert with their respective ID.

Answer №5

let identifiers = []; //initialize an empty array
$('.failed').each(function(e)){   //loop through each element
    identifiers.push($(this).prop('id')); //add their ID to the array
});
alert(identifiers); 

Answer №6

If you loop through the input elements, you can perform any action on those that belong to a specific class:

$("input.failed").each(function() {
  console.log($(this).attr('id'));
});

Answer №7

To trigger a notification for all IDs when the button is clicked, you can use this code:


$("#btnid").click(function(){    // Click event on button
    $('.failed').each( function () { 
       alert ($(this).attr('id')); // Display ID one by one in an alert
  });
});

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

Tips on Getting Your Contact Form Operating Smoothly

Hey everyone, I'm exploring the world of web design and have a question about getting my contact form to work on my current theme. Below is the HTML code I am using: I would greatly appreciate it if someone could help me with coding the PHP file; doe ...

What is the best way to implement ajax and PHP for generating and filling div elements on a web page?

I recently posted a question on Stack Overflow that helped me understand the basics of using Ajax and jQuery to interact with a PHP database. However, I'm still facing some challenges and need help in refining my approach. In my database connection, ...

Creating an addition button using jQuery on-the-fly

Looking to implement pagination using jQuery and Ajax. The first hurdle I've come across is figuring out how to add a button dynamically. <div class="btn-group" id="pagination_group"> <button class="btn" disabled="disabled">First</ ...

What could be causing the problem with my CSS background-image being referenced correctly?

Everything seems to be in order: background-image: url(./dir/img.jpg); However, I encounter a 404 error when attempting to reference an image using this approach: index.html <div style="--url: url(./dir/img.jpg);"></div> css backg ...

Struggling with dynamically updating fields based on user input in real time

I have a goal to update a field based on the inputs of two other fields. Currently, all the fields are manual input. Below is the code I'm using to try and make the "passThru" field update in real-time as data is entered into the other fields. The ma ...

Elegant Modal Form Submission Using jQuery

Having trouble submitting a modal jQuery form? A code snippet was borrowed from a website with minor modifications and the JavaScript was moved to a separate file. The challenge lies in transmitting form data to a PHP script via AJAX. The serializedArray() ...

Exploring the dynamic world through HTML5 canvas and animated objects

Today I am exploring HTML 5 canvas and experimenting with moving 3 circles on the canvas. Based on my research, it looks like I need to continuously redraw the circles (perhaps every 60 milliseconds) and clear out the old circle before rendering the new on ...

the process of configuring date string for x-axis labels in chartjs

I recently created a time scale chart using chart.js. However, I now want to change the labels in my data array to be in the format of 01-02-2017, 02-06-2017 instead of "4 February 2017", "9 February 2017" and so on. Below is my code: var aR = null; va ...

Is there a way to eliminate this from the footer section?

Check out my website: If the text is not visible, try using a larger monitor or zooming out in your browser. I've added an image to help explain: I only want to remove that element + title on the first page This is the HTML code: <div id="to ...

Navigating a roster of users in JavaScript

I'm dealing with a collection of user data stored in an array accountsade: [ { id: 0.4387810413935975, name: "Adrian", password: "345", userName: "nathanael" }, { id: 0. ...

Trouble with executing action after submitting form using jQuery and AJAX

I'm currently working on implementing form validation and submission using jQuery and AJAX. For the most part, everything is functioning as expected. However, there is a small snippet of code causing an issue: alert ('Validation success'); ...

Having trouble appending array elements to table rows using jQuery

I'm currently working on a project that involves creating a table using jQuery. I've encountered an issue when trying to populate the table rows with integer elements from an array. Unfortunately, I'm receiving the following error message ...

Incorporate dynamic body color changes across all pages using AngularJS

On the home page, I want to change the color scheme so that it is consistent across all pages. The user should be able to choose a color from a list on the home page and have it apply to every page. Currently, the color selection only applies to the home p ...

I'm having trouble getting a CSS transition to work on a PNG sequence using jQuery in Mozilla

For my latest project, I decided to create an animation using a PNG sprite sequence. The idea was to add a smooth transition effect on hover and have the animation play in reverse when the hover state ends. If you want to take a look at the code yourself, ...

Display/Conceal Title with Hover Effect in Fancybox2

I need assistance with hiding and showing the title in fancybox2 using the hover event. The code I have tried is not working properly. Here is my current code: $("#fancybox-wrap").hover(function() { $("#fancybox-title").show(); }, functio ...

What is the best way to instruct jQuery to disregard an empty server response?

After examining this piece of code: $.ajax({ type: "POST", url: theRightUrl, data: whatToPost, logFunction: whatever, suppressSuccessLogging: !0, dataType: "html" }); I encountered an issue where Firefox displays a "no element ...

preserving the current state even after refreshing the page

Currently, I am teaching myself React. When I click on the favorites button, which is represented by a heart symbol, it changes color. However, upon refreshing the page, the color change disappears. To address this issue, I came across the following helpfu ...

Navigate to the location using AngularJS elements in the JavaScript iframe1

Dealing with an issue while working on AngularJS. I am facing a frustrating problem where I am attempting to use one link to open links in two separate iframes. All aspects of this function correctly, except for the actual links. The issue arises when usin ...

Easiest method to change cursor to 'wait' and then return all elements to their original state

On my website, there are certain CSS classes that define a specific cursor style when hovered over. I am trying to implement a feature where the cursor changes to a "wait" image whenever an AJAX call is initiated on any part of the page, and then reverts b ...

Navigating between two intervals in JavaScript requires following a few simple steps

I have created a digital clock with a button that switches the format between AM/PM system and 24-hour system. However, I am facing an issue where both formats are running simultaneously, causing the clocks to change every second. Despite trying various s ...