0

As jQuery .css() dont work if I want to get css value of margin, padding.

I found a jquery code which solve this this bug.

Please help me to extednd jquery .css() by adding this code:

$.each(['border', 'margin', 'padding'], function (i, name) {
        $.fn[name] = function (value) {
            if (value) {
                if (value.top !== undefined) {
                    this.css(name + '-top' + (name === 'border' ? '-width' : ''), value.top);
                }
                if (value.bottom !== undefined) {
                    this.css(name + '-bottom' + (name === 'border' ? '-width' : ''), value.bottom);
                }
                if (value.left !== undefined) {
                    this.css(name + '-left' + (name === 'border' ? '-width' : ''), value.left);
                }
                if (value.right !== undefined) {
                    this.css(name + '-right' + (name === 'border' ? '-width' : ''), value.right);
                }
                return this;
            }
            else {
                return {top: num(this.css(name + '-top' + (name === 'border' ? '-width' : ''))),
                        bottom: num(this.css(name + '-bottom' + (name === 'border' ? '-width' : ''))),
                        left: num(this.css(name + '-left' + (name === 'border' ? '-width' : ''))),
                        right: num(this.css(name + '-right' + (name === 'border' ? '-width' : '')))};
            }
        };
    });

Hre is the fiddle which shows that jquery .css() fails getting value of padding and margin, in firefox browser

http://jsfiddle.net/howtoplease/fMTsW/4/

2
  • 1
    What are you asking? Did you try the code? Did it not work or something? Commented Jul 19, 2013 at 4:42
  • yes work in chrome but fails in firefox, I want to use above code to extend css() function Commented Jul 19, 2013 at 4:45

1 Answer 1

1

i have a solution, have a look at this.

$(document).ready(function(){
       ....
           $('[name='+this.name+']').val(marginT);
           .....
       }else{         
        this.value = $('h3').css(this.name);
       }
    });
});

WORKING DEMO

The above code is my own logic, I have also implemented the code that you have provided.

Your Plugin function

This plugin also has issue, check the console.log displayed.

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

1 Comment

demo fails if margin:40px 50px;

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.