Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7570035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:19:00+00:00 2026-05-30T15:19:00+00:00

I am iterating over input elements using .each() and want to get class of

  • 0

I am iterating over input elements using .each() and want to get class of each input element.

$("input").each(function(index,element){
 // Here I want to access class value of current input ie. element here (I guess) 
});

For example

HTML

<input name="name1" class="class1" value="input1">
<input name="name2" class="class2" value="input2">

I tried following selectors in .each but getting return as undefined

 1. alert($('this').attr['class']);  
 2. alert($(this).attr['class']);      
 3. alert(element.class);  // I can access name using element.name
 4. alert($(element).attr['class']);

Jsfiddle : http://jsfiddle.net/kGjr2/
How can I access respective input and its properties inside .each()?

Thanks!


Really amazed by the quantity and quality of answers and that to within a really short time. Just wondering which answer should I accept. Thanks you all.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T15:19:02+00:00Added an answer on May 30, 2026 at 3:19 pm

    Just use parenthesis instead of square brackets… you are calling functions:

    $("input").each(function(index,element){
        alert($(this).attr('class'));
    });
    

    You can also select the elements by class:

    $('.class1')...//do something to only the elements with the `class1` class
    

    Here is an updated version of your JSFiddle: http://jsfiddle.net/kGjr2/1/

    Update

    The reason you are getting undefined alerted at you is because square brackets are for accessing a property of an object/array. But since you are using the square brackets on a function you aren’t going to find any property names that match class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I am iterating over each file using : @echo off FOR %%f IN
I am iterating over a list and I want to print out the index
How do I access the index while iterating over a sequence with a for
I am iterating over an table to pick out the form elements from each
When iterating over elements of a vector it is preferred to use iterators instead
When using a SortedDictionary in Linq and iterating over the KeyValuePair it provides, can
Both Junit and TestNG provide mechanisms for iterating over a collection of input parameters
I'm trying to develop a function which will allow me to input new elements
Using scipy's interpolate.splprep function get a parametric spline on parameter u , but the
Iterating over a queryset, like so: class Book(models.Model): # <snip some other stuff> activity

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.