Toniq
Toniq

Reputation: 4996

Javascript / jQuery change inline style values

I have an inline css in my page:

<style id="abc">
 .player-holder{
    position:relative;
    width:100%;
    height:40px;
 }
 .icon-color{
    color:#fff;
   -webkit-transition: color 0.3s ease-in-out;
   transition: color 0.3s ease-in-out; 
 }
 .icon-rollover-color{
   color:#fff;
   -webkit-transition: color 0.3s ease-in-out;
   transition: color 0.3s ease-in-out; 
 }
</style>

Is it possible to change some values on the fly (using jquery/javascript) so that browser takes change immediately?

Like change:

.icon-rollover-color

to

color:#333;

Upvotes: 3

Views: 8106

Answers (4)

Sascha
Sascha

Reputation: 635

You, indeed this is possible!

Just add via Javascript or jQuery the following style-tag on the bottom of your page.

<style type="text/css">
.icon-rollover-color {
    color:#333!important;
}
</style>

You can add such codes to the bottom before the body-tag closes with the append-command:

$(body).append('<style type="text/css">.icon-rollover-color {color:#333!important;}</style>');

Upvotes: 0

Sam Anderson
Sam Anderson

Reputation: 300

Yes, you can change inline CSS using the jquery .css() function. See this Fiddle.

$('p').css('color','#00FF00');

If you are dynamically adding elements then I would suggest you write this as a function that is called whenever a new element is added, probably using an event listener, that you can pass your updated style values to as parameters. Something like:

updateDOMStyle('<style>','<value>');

Upvotes: 3

You can do that by using simple jquery.

$('.icon-rollover-color').css('color','red')

Upvotes: 1

Rachel Gallen
Rachel Gallen

Reputation: 28553

in jquery

<script>

  $('.icon-rollover-color').css({
    'color': '#333'
  });

</script>

in javascript

<script type="text/javascript">
document.getElementByClassName(".icon-rollover-color").style.color="#333"
</script>

Upvotes: 1

Related Questions