1

i have a style on header empty like

<style> 
.cls{
}
</style>

I want to change it dynamic by click example to

<style> 
.cls{
 font-size: 15px;
 font-family : Arial, Verdana;
 border: 1px solid blue;
 background-color: yellow; 
}
</style>

Here is my code http://jsfiddle.net/nyf7T/

<script>
 function changeCSS () {
 var style = 'font-size: 15px;'
 + 'font-family : Arial, Verdana;'
 + 'border: 1px solid blue ;'
 + 'background-color: yellow;'; 
 alert(style);
 }
</script>
<style> 
.cls{
}
</style>
<div>
 <table style="margin:5px;">
 <tr>
 <td class="cls">
 abcd
 </td>
 </tr>
 </table>
</div>
 <a href="#" onclick="changeCSS();">Setstyle</a>

How to do that thank.

asked Sep 6, 2013 at 9:20
2
  • If JQuery is allowed, you can use addClass() to add the style to your element. Commented Sep 6, 2013 at 9:23
  • Do you really want to modify CSS declarations or just the style of a particular element? Commented Sep 6, 2013 at 9:44

6 Answers 6

2

What you have asked is to change a rule of a CSS class.

You need to use CSSStyleSheet.insertRule method to do this. More info here

You might also need to look at these

document.styleSheets for getting the style sheets used in the page.

styleSheet.cssRules for getting the cssRules in a particular style sheet.

rule.selectorText property -> Check here

In your case you need to try something like this (or a more refined version)

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
.cls {
}
</style>
</head>
<body>
<div>
 <table style="margin:5px;">
 <tr>
 <td class="cls"> abcd </td>
 </tr>
 </table>
</div>
<a href="#" onClick="changeCSS();">Setstyle</a> 
<script>
 function changeCSS () {
 var myStyleSheet = document.styleSheets[0];
 var style = '.cls { font-size: 15px;'
 + 'font-family : Arial, Verdana;'
 + 'border: 1px solid blue;'
 + 'background-color: yellow; }'; 
 myStyleSheet.insertRule(style, 0);
 }
 </script>
</body>
</html>

Check the working version here http://jsfiddle.net/dK65G/1/

answered Sep 6, 2013 at 10:14
Sign up to request clarification or add additional context in comments.

1 Comment

What real-world scenarios would require this behavior?
1

You can use the jquery css function:

here is the example: http://jsfiddle.net/nyf7T/6/

<script>
$(function(){
 $('.changecss').click (function(){
 $('.cls').css({
 'font-size': '15px',
 'font-family' : 'Arial, Verdana',
 'border': '1px solid blue',
 'background-color': 'yellow'
 });
 });
});
</script>
answered Sep 6, 2013 at 9:41

Comments

1

In your case you can use the below code

var style = '.cls{font-size: 15px;'
 + 'font-family : Arial, Verdana;'
 + 'border: 1px solid blue;'
 + 'background-color: yellow;}'; 
document.getElementsByTagName('style')[0].innerHTML=style;

http://jsfiddle.net/nyf7T/8/

answered Sep 6, 2013 at 10:02

Comments

1

Consider below piece of jQuery

$('.cls').css({
 "font-size":"15px",
 "font-family":"Arial",
 "border":"1px solid blue",
 "background-color":"yellow"
});

cheers :-)

answered Sep 6, 2013 at 9:43

Comments

0
<style> 
.cls{
 font-size: 15px;
 font-family : Arial, Verdana;
 border: 1px solid blue;
 background-color: yellow; 
}
.newCls {
 font-size: 15px;
 font-family : Arial, Verdana;
 border: 1px solid blue;
 background-color: yellow;
}
</style>
<a href="#" onclick="$(this).removeClass('cls').addClass('newCls');">Setstyle</a>
answered Sep 6, 2013 at 9:29

Comments

0

You can put the code into a class.

And set the class to the element onclick:

 function changeCSS () {
 $( "element" ).addClass( "theclass" );
 }

In CSS:

.theclass {
 font-size: 15px;
 font-family : Arial, Verdana;
 border: 1px solid blue ;
 background-color: yellow;
}

if you want to change the clicked element do this:

function changeCSS (this) {
 $(this).addClass( "theclass" );
}
<a href="#" onclick="changeCSS(this);">Setstyle</a>

The clou is to set up the CSS before you allocate it.

answered Sep 6, 2013 at 9:22

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.