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

  • SEARCH
  • Home
  • 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 7185713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:28:32+00:00 2026-05-28T18:28:32+00:00

I have some DIVs on a page. Each has 3 classes: grid_#, push_# and

  • 0

I have some DIVs on a page. Each has 3 classes: grid_#, push_# and pull_# where # is an actual number.

I need to replace each class separately, i.e. remove class=’grid_1′ and replace it with class=’grid_14′.

I’m using a select list to choose the new class. I need the name of the old class so I can remove it.

Through trial and error, I’ve found that as long as an element has only one class, I can get it using

var old = document.getElementById('elementId');
var old_class = old.className;

When I try to get the class name from a DIV with multiple classes, I get nothing. Not undefined, not object, just blank. I’ve tried var old_class = old.className[0]; among other things, but that gives me undefined.

The current plan is to use regex to match the old class name to a list of possible classes, giving me a var with the class name to be removed. Being new to javascript I’m sure there is a better way to do it. Any suggestions are more than welcome.

I’ve just begun this project, but you can find it here if it would help.

Thanks,
Mark

EDIT: Changed title of question

  • 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-28T18:28:33+00:00Added an answer on May 28, 2026 at 6:28 pm

    If element has multiple classes, you need to use split:

    var old = document.getElementById('elementId');
    var old_class = old.className.split(' ')[0]; // first class
    

    What split does is that it creates an array by splitting string (class in this case) which you can later access by its index eg [0] for first class.

    If element has just one class, you simply use className:

    var cls = old.className;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a page in asp.net MVC3 project. The page has 3 divs, each
I have some divs: <div class=A>Target</div> <div class=A B>NotMyTarget</div> <div class=A C>NotMyTarget</div> <div class=A
I have a series of DIVs on a page which each contain a nested
I have a few different divs with different projects and names. Each one has
I have some divs that when the page loads are populated with images of
I have some data wrapped in 3 divs, each div represents a 'tab', and
I have div s with class someclass; each div has child div s with
I have some DIVs that I am using JQuery to hide and show using
I have some icons inside divs and needed to lay them out with a
I have the following code snippet embedded into some of my divs so when

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.