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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:16:25+00:00 2026-05-28T03:16:25+00:00

Changing class for DIV through JavaScript works great until I interrupt it by changing

  • 0

Changing class for DIV through JavaScript works great until I “interrupt it” by changing style attribute for that DIV.

case 0: changes left to 400

case 1: changes left to 600

case 2: doesn’t work!

Why’s that?

<!DOCTYPE html>
<html>

<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <style type="text/css">
        .pos1 {left: 200px; top:400px;}
        .pos2 {left: 400px; top:400px;}
        .pos3 {left: 800px; top:400px;}
    </style>
</head>

<body>
        <div id = "img" class = "pos1" style="position:absolute;"> <img src="gfx/1n.png"> </div>


        <script type="text/javascript">
            var action = 0;
            window.onmousedown = function(event){makeAction();}

            function makeAction() {
                switch(action) {
                    case 0: action++; document.getElementById('img').className = "pos2"; break
                    case 1: action++; document.getElementById('img').style.left = "600px"; break;
                    case 2: action++; document.getElementById('img').className = "pos3"; break;
                }

            }

        </script>
</body>
</html>
  • 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-28T03:16:26+00:00Added an answer on May 28, 2026 at 3:16 am

    Because the style attribute you set in the second case overrides the style from the class you assign in the last case.

    After this first case the image tag is

    <img class="pos2" ... />
    

    So it’s positioned left by 400px.

    After the second case the image tag is

    <img class="pos2" style="left: 600px" ... />
    

    Even though it still has class pos2, the style attribute overrides the left positioning, so it’s now positioned left by 600px.

    After the last case the image tag is:

    <img class="pos3" style="left: 600px;" .. />
    

    Even though it now has class pos3, the style attribute still overrides the left positioning, so it remains positioned left by 600px.

    Understand that CSS does not depend on when it’s applied, but that it always follows the rules of specificity. And the rules state that style rules in the style attribute of a tag override style rules from a class.

    You can override the rule using the important keyword:

    .pos3 {left: 800px !important; top:400px;}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this current script which works great for changing class names $('#mydiv li
inheriting a class attribute from a super class and later changing the value for
I'm trying to improve my Javascript coding style and have been reading that it's
changing a static resource during runtine is something that sounds not possible. I have
html- <div class=hbg> </div> some menu items here jquery for mouser over on menu
I would like to have a div which changes its content on the page
I have a block of code through which I am changing the title of
I have html that looks like this, <div> <span>Text <a href=example.html>ABC</a> <a href=example.html>DEF</a> <a
I have the following html code: <div class=trial1> He said all but the three
I have a div with class divItemclass for this class, I have put height

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.