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 7851379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:09:58+00:00 2026-06-02T19:09:58+00:00

when I am using jQuery .attr(‘class’), I got back ‘class1 class2 class3’, they are

  • 0

when I am using jQuery .attr(‘class’), I got back ‘class1 class2 class3’, they are supposed to be separate classes, how do I select one of the class, for example I want to change .css on class1, how would I iterate through the returned class names, are these a string that I have to split, or are these objects? Thanks!

BTW,
My html looks like this:

<td class ="class1 class2 class3">

I got it working by using .spilt on the returned .attr(‘class’). My goal is to get the second class, and do some .css() on it.

  • 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-06-02T19:10:01+00:00Added an answer on June 2, 2026 at 7:10 pm

    The class or className property returns a space separated list of class names in a single string. If you want to manually operate on that attribute, then you have to manually parse the string yourself into it’s resulting class names. It’s a string, not individual objects.

    Now, fortunately when you’re using jQuery, you rarely have to do that yourself. If you want to add, remove or change a single class name, you can use some combination of these jQuery methods which will do all the parsing for you:

    .hasClass(classname)
    .replaceClass(oldName, newName)
    .removeClass(classname)
    .addClass(classname)
    

    To remove one class and add a separate one, it’s not uncommon to chain two functions together like this.

    x$.removeClass("foo").addClass("bar");
    

    If you really just want the class name to be used in a selector, then you don’t have to worry about how many other class names there are on the object at all as the selector library built into jQuery will handle all that for you. For example:

    $(".myClassName").css("color", "red");
    

    will find all objects that have myClassName anywhere in the className property and will apply the specified CSS to those objects.

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

Sidebar

Related Questions

I'm using JQuery to switch out an image src thusly: $(#myImg).attr(src, ../../new.gif); notice the
I have a script on jquery that load some site in iframe using: $(#demo_frame).attr('src',
Am trying to change text of an asp:Button using jQuery Like this $(#<%=delButton.ClientID%>).attr('text', 'InActivate');
How to write $('label.someClass').attr('valid', true); in Java Script with out using jQuery?
I'm using jquery to add rel=brochure using $('.imageOuter a').attr('rel', 'brochure') this works as expected.
I have the following code, written using jQuery: var strval = '22' $(#quicknote).attr(href, javascript:location.href=/'http://site.com/notes/add?projects=/'+
I am using jQuery(something).attr(id) in jQuery 1.6.1 and seem to be getting issues with
I am using jQuery's Address plugin ( website ) to enable the back/forward buttons
I'm using jQuery to update an image source $(#myImage).attr(src, imagePath); Immediately after, I try
I'm using $(#my_img).attr(src, /another_img.png); inside the beforeSend callback of an Ajax request using jQuery,

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.