5

I know how to apply css on a single tag (or changing class on it) but THIS IS NOT WHAT I AM ASKING!

I would like to modify an attribute within a CSS class without touching the element where the class is applied.

In other words if this is the css

.myclass {
  font-size: 14px;
  color: #aab5f0;
}

and this is the html

<span id="test" class="myclass"> foo bar </span>

to increase the font of the span tag I want to modify the content of the class and NOT doing something like

var fontsize = parseInt($('#test').css('font-size').replace(/[^-\d\.]/g, ''));
fontsize += 10;
$('#test').css('font-size', fontsize+'px');

I want that the class becomes

.myclass {
      font-size: 18px;
      color: #aab5f0;
    } 

Is there a way to change the actual class through Javascript?

My other solution would be to put the css in a container and refill the container each time, but I have the sensation that it is not a good idea...

Giovanni Di Milia
  • 13,480
  • 13
  • 55
  • 67
  • If it really is just for one element, then stick with exactly what you have. In fact, I'm not sure why you'd ever want to "change the class". Though, it is possible. – thirtydot Oct 05 '11 at 20:11
  • 1
    No, it's not for only one element, it is for 100 different span tags that are created dynamically by a function and placed in the page with absolute positioning to create a "cloud". The problem is that I cannot touch the span (I mean, it is useless) because I need to re-run the function to create and place again the new bigger words in the page to be sure that the words will not overlap. – Giovanni Di Milia Oct 05 '11 at 20:19
  • A little niceness goes a long way... Because so many ppl aren't understanding the question (myself included) maybe your wording of the question needs more work and less bolding – Mike Fielden Oct 05 '11 at 20:38
  • 1
    I changed the question when I understood that it was not clear, but if I say "without touching the span tag" and everybody answer "apply a new style to the span tag", sorry but I don't know how to be more clear! – Giovanni Di Milia Oct 05 '11 at 20:45
  • I'm thinking you need to take a long, hard look at the problem, and figure out whether this is *actually* what you want. – Ryan Kinal Oct 05 '11 at 21:07

6 Answers6

6

I think ideally you should define a base font size on the html or body element. All of the other font sizes in your CSS should be relative to that "master" font size, like so:

Then, when you adjust the font-size of the body through jQuery.css(), the other elements will all automatically adjust their size relative to the parent.

$(body).css('font-size', '14px');

You don't have to use the body level, you could define base font-size a div or other container and use that as the parent instead.

Here is a contrived example of this in action:

$('#grow').click(function() {
  $('body').css('font-size', '18px');
});
body {
  font-size: 12px;
}

h3,
p {
  font-size: 1.0em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<h3>Main Title</h3>
<p> Welcome to my website. </p>

<button id="grow" type="button">Grow</button>
Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
2

My comment notwithstanding, look at this question: Setting CSS pseudo-class rules from JavaScript

Changing a class and "setting pseudo-class rules" are achieved in the same way.

@Box9's answer is probably the one you should actually use:

I threw together a small library for this since I do think there are valid use cases for manipulating stylesheets in JS.

Community
  • 1
  • 1
thirtydot
  • 224,678
  • 48
  • 389
  • 349
0

looking for same thing.

This was my solution

https://jsfiddle.net/sleekinteractive/0qgrz44x/2/

HTML

<p>The Ants in France stay mainly on the Plants</p>

CSS

p{
font-family:helvetica;
font-size:15px;
}

JAVASCRIPT (uses jQuery)

function overwrite_css(selector,properties){ 

// selector: String CSS Selector
// properties: Array of objects

var new_css = selector + '{';

for(i=0;i<properties.length;i++){

new_css += properties[i].prop + ':' + properties[i].value + ';';

}

new_css += '}';

$('body').append('<style>' + new_css + '</style>');

}

overwrite_css('p',Array({prop: 'font-size', value: '22px'}));
overwrite_css('p',Array({prop: 'font-size', value: '11px'}));
overwrite_css('p',Array({prop: 'font-size', value: '66px'}));

// ends on 66px after running all 3. comment out lines to see changes
0

There is no need to change the class itself. To override the behaviour, simply add another class to the existing one or change it entirely. I'll show the first option:

.myclass {
    font-size: 14px;
    color: #aab5f0;
}
.myclass.override {
    font-size: 12px;
}

And then all you need to do from javascript is to toggle the override class. This way it's also much better as all the presentation is done in CSS.

deviousdodo
  • 9,177
  • 2
  • 29
  • 34
  • 1
    I explicitly said that I don't want to touch the span tag. I know how is the best way to do this, but I cannot apply this technique. BTW, this doesn't work even in my example! – Giovanni Di Milia Oct 05 '11 at 20:23
  • 1
    I actually missed that line, sorry. But now looking again, I don't get it how adding a class "touches" the tag, but applying styles directly to it doesn't. And why do you say it's not working in your example? – deviousdodo Oct 05 '11 at 20:30
  • 1
    The problem is that what I want to do is to increasing the size of several span tags generated dynamically and placed in the page according to their dimension. Now if I apply a different class or add another one as you suggest, when I re-create the span tags I'm going to loose the information I applied before. – Giovanni Di Milia Oct 05 '11 at 20:42
  • I responded above to this, but this is getting a bit awkard :) I think the best thing would be to just close this question and open a new one related directly to your specific use case. Because there are ways of keeping the presentation together with the content, but there's no point arguing over a generic case. – deviousdodo Oct 05 '11 at 20:48
  • 1
    Actually I think that looking for a method to change the CSS globally without querying the DOM is a good idea! Look for the small library written by thirtydot for example... – Giovanni Di Milia Oct 05 '11 at 21:00
  • Without querying the DOM? Have you actually looked at the library? It adds new stylesheets to the DOM with your Javascript defined rules (which is impressively abusive). Anyway, if you think that meddling with styles from Javascript is a good ideea, anything works I suppose. – deviousdodo Oct 05 '11 at 21:23
  • I actually read the code after I answered to your comment and I agree that that class is abusive. – Giovanni Di Milia Oct 05 '11 at 21:40
0

Since it's obvious you're using jQuery, please see the addClass and removeClass functions.

$('#test').removeClass('myClass').addClass('yourClass');
Ryan Kinal
  • 17,414
  • 6
  • 46
  • 63
  • 1
    Which part of NOT TOUCHING THE SPAN TAG is not clear? I don't want to touch the span tag, I know how to solve the problem with this technique, but I don't want to do it in this way! – Giovanni Di Milia Oct 05 '11 at 20:26
  • 1
    "to increase the font of the span tag I want to change the class and NOT doing something like" ... hence, change the class. I guess I just don't understand the difference between changing the class on the span, and changing the class in the CSS. If nothing else, then simply use an additional class with a different fontsize. (just use .addClass('biggerFontSize') or something equally as banal). – Ryan Kinal Oct 05 '11 at 20:31
  • 1
    I mean that I don't want change the class name of the span tag, I want to change the attribute "font-size" inside the class. The difference is that In another piece of code I'm going to delete the span tag and re-create it and place it in the page according to its size. If I change the font size inside the element (or the class name) when I do what I just described, I'm going to loose the change and having the default values! – Giovanni Di Milia Oct 05 '11 at 20:38
  • So what you actually want is a property assigned to plain text. Unfortunately that's not possible. There are ways to solve what you described though, the simplest would be to simply move the classes along with the text, if you are interesting in moving the presentation along with the content. – deviousdodo Oct 05 '11 at 20:44
  • My confusion here is that all of your spans will have the same class. If you are somehow able to modify the CSS properties within the rules for that class, then all of your spans will change, resulting in *all* of your spans changing their display. So, instead of sorting by font size, why not sent a partly-numeric class (i.e. myclass25, myclass26, etc.), and sort by that? – Ryan Kinal Oct 05 '11 at 21:04
-1

Simply JQuery:

$('#test').attr('class','bigFont'); 

or native JS :

document.getElementById('test').className ='bigFont';

UPDATED

var size = [ 10 , 20 , 30 ] ;
var i =0;
$('#test').css('font-size',size[i]+'px');
i = (i+1)%size.length ; 
shox
  • 1,150
  • 5
  • 18
  • 32