Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery css function not working across all browsers

Tags:

jquery

css

Given this HTML, CSS, and JavaScript: http://jsfiddle.net/XvqYS/3/

If you run it on Google Chrome it alerts

10px

Running it on IE, Safari, or FireFox alerts and empty string. Why?

I'm using IE9, Chrome 18.0.1025.168, FireFox 12.0, Safari 5.0.4

like image 565
Jonas Stawski Avatar asked May 27 '26 16:05

Jonas Stawski


1 Answers

Individual properties, such as margin-left have to be used.

The basic method to read all properties is: http://jsfiddle.net/XvqYS/8/

$(function () {
    var $main = $("#main");
    var margin = [
        $main.css('margin-top'),
        $main.css('margin-right'),
        $main.css('margin-bottom'),
        $main.css('margin-left')
    ].join(' ');
    alert(margin);
});

You can tweak it to return a compact value, eg. 10px 10px 10px 10px -> 10px.

like image 189
Rob W Avatar answered May 30 '26 05:05

Rob W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!