0

I have a form that allows the user to push an up or down button to resize the font in an element. When I reset the form I need to put the "default" font size back in a hidden field to send to a php script. So how can I get the css font size actually typed into the css stylesheet, not the one that has been altered by jquery?

Thank you, Todd

2
  • Where is the code that resizes it? What is the purpose of getting the default value, just to reset the presentation? Commented May 27, 2012 at 13:03
  • yes, to reset everything as in a 'fresh' reload. Commented May 27, 2012 at 16:50

3 Answers 3

2

The easiest way would be to store the original font size before you change it:

var originalFontSize = $myElement.css('font-size');

Then in your code for resetting the form:

$myHiddenElement.attr('value', originalFontSize);

You can access definitions within the actual stylesheet using Javascript - described here - but then you'd have to make all sorts of assumptions about where the relevant definition for your element is located and wouldn't take into account any browser-specific overrides etc. It's much easier to just grab the state of that element before you modify it.

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

2 Comments

This is what I ended up doing but it seemed a little wrong. It is probably the easiest and most reliable. Thanks.
@maddogandnoriko - See Steve's answer below - he's shown that setting the fontSize property to null reverts it to its stylesheet value. It might be more compatible across browsers to use jQuery's version of this, i.e. $myElement.css('font-size', '')
1

You can define the default font size in your css like so:

#myelement {
    font-size: 12;
}

Then you can override this value locally by using javascript

var element = document.getElementById('myelement');
element.style.fontSize = new_font_size;  // local values override the value from the stylesheet.

Then when you want to set it to the default

var element = document.getElementById('myelement');
element.style.fontSize = null; // now it reverts back to using the value defined in the stylesheet

Comments

0

You need to set the default value in JS variable before changing font-size.

var defaultFontSize = 20;

And then use it to do reset.

2 Comments

but then if I change it in the stylesheet down the road I will have to remember to change it in the js also....
undetermined....but if I decide to change it a year from now I am likely to forget to change it in both places

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.