0

Consider the following

<style type ='text/css'>
   .c1
   {
        opacity:0.3;
   }
</style>

and

<div class = 'c1' id = 'myDiv'></div>

and

<script>
   function cssFn()
   {
       document.getElementById('myDiv').style.opacity = 0.8;
   }
   cssFn();

</script>

Thus i changed the opacity of the element using javascript.. My question is this.Is it possible to make the style declaration done using javascript to be inactive and that declared in class to be active using javascript..I dont need

document.getElementById('myDiv').style.opacity = 0.3;

I just need a code which resets the style to that declared using stylesheet

1

3 Answers 3

0

I don't know if this can easily be done. Changing the styles in javascript creates an inline style on the element... so <div style="opacity: 0.3; "> you can remove the attribute or you can !important all things (do not do the latter). However either solution is hacky.

or as has been suggested in the other question you can set the property to "". which would negate the rule and take the stylesheet rule.

Sign up to request clarification or add additional context in comments.

4 Comments

i tried the "" and null method ..Both of them is not working
element.setAttribute('style', '');
but that will also remove the previous style declaration... right?
the more and more i look into how to negate inline styles and force the external css declarations the more and more I see without physically removing the inline style there is no way.
0

You can try to use 2 different classes.

<style type="text/css">
  .c1 {
   opacity: 0.3;
  }
  .c2 {
   opacity: 0.8;
  }
</style>

You can do with javascript :

 document.getElementByID("myDiv").className = "c1";

Or with jQuery :

$("#myDiv").addClass("c1");

1 Comment

no ...that's not what i need...I just need to make the decalaration using javascript inactive and other declarations including inline,id(ex:#myDivin style),tag(ex:<div> in style) to be active instead of the previous declaration using javascript... The actual problem is this: i've created a fadeIn function which does a fade In effect using css3...After the effect shown i need to change the opacity back to the original one....
0

Put the style that you want to dynamically add or remove via Javascript in a separate class, then use Javascript or jQuery to add / remove that class when you need it.

So, along the same lines as what Andredseixas started with:

<style type ='text/css'>
   .c1
   {
     opacity:0.3;
   }
   .c2
   {
     opacity:0.3;
   }
</style>

In your function cssFn, using jQuery - do this instead of manually changing the opacity via the attribute:

$("myDiv").addClass("c2");

Then, using jQuery when you are done with your fadeIn logic:

$("#myDiv").removeClass("c2");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.