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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:38:09+00:00 2026-05-26T13:38:09+00:00

How can I change a div class name when clicking on it? Eg: <div

  • 0

How can I change a div class name when clicking on it? Eg:

<div class="first_name" onclick="changeClass();" id="first_name"></div>

I want to change it as follows, when a user clicks on the div

<div class="second_name" onclick="changeClass();"></div>

I wrote the JavaScript as:

<script language="javascript"> 
  function change_autorefreshdiv(){
    var NAME = document.getElementById("first_name")
    NAME.className="second_name"
  } 
</script>

It’s working for the first instance only. That is on page load, if I click on it, the first_name gets changed into second_name. But clicking on it again, it won’t revert the second_name to first_name.

  • 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-26T13:38:10+00:00Added an answer on May 26, 2026 at 1:38 pm

    You have to define the second class name. Currently, you have got a function which changes the class name to a hard-coded value, independent on the current class name. See also: MDN: if…else

    function change_autorefreshdiv(){
        var NAME = document.getElementById("first_name");
        var currentClass = NAME.className;
        if (currentClass == "second_name") { // Check the current class name
            NAME.className = "first_name";   // Set other class name
        } else {
            NAME.className = "second_name";  // Otherwise, use `second_name`
        }
    }   
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two divs as follows and I want to change the class name
Here is the div element on my page: How can I change a class's
Suppose to have a code like this: <div class=notSelected> <label>Name <input type=text name=name id=name
I want to create user profile with image. That user can upload his photo,
I am using prettyCheckboxes and I want to change it so that the div
Its work fine but when user change 'ProductDTO.Property1' field name to 'ProductDTO.Property2' - via
I am trying to make a layout switcher. A user clicks a certain div
Some very helpful folks wrote and then modified a script to change the class
i have this model on mvc: public class User { public string Name {
You can change the connection string at run-time like this. You make the connection

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.