April 3, 2012

Changing .css in jQuery using static values

Question by jsuissa

I used the PHP below using regex to resize the font-size/line-height of an html page.
I’d like to create similar fucntionality in jQuery.

Is there a way to store static values for an entire method like .css() so each time the scale calculations are based off the original value instead of their current value?

jQuery code excerpt:

$("#resume_holder").contents().find('body').children().css('font-size', '+=' + ui.value);

PHP Code:

private function _font_size($output,$scale,$line_height_scale =  FALSE) {

        $callback = new Font_Callback($scale);
        $pattern = '%(bfont-size:s*)([0-9]+)%s';
        $output  = preg_replace_callback($pattern, array($callback, '_alter_font_size'), $output);  

        if ($line_height_scale === FALSE) {
        $callback = new Font_Callback($scale);
        } else { $callback = new Font_Callback($line_height_scale); }
        $pattern = '%(bline-height:s*)([0-9]+)%s';
        $output  = preg_replace_callback($pattern, array($callback, '_alter_font_size'), $output);  
        return($output);
}

Answer by Starx

You can use jQuery’s .data() for that.

$("img").data("yourvar", "yourvalue");

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!