0

the following does NOT work:

function doRestyle(div) {
        jQuery(div).css({
            "margin-top": addableMargin + "px",
            "margin-bottom": addableMargin + "px",
        });
}

However, this works:

function doRestyle(div) {
        jQuery('#idName').css({
            "margin-top": addableMargin + "px",
            "margin-bottom": addableMargin + "px",
        });
}

Any explanation? Many thanks :)

4
  • 1
    It should work if the div is e.g. "#idName". Show how do you call doRestyle function. Commented Nov 5, 2013 at 10:26
  • 2
    Note that the trailing comma in your object literal will cause some older browsers to fail. Best to remove it. Commented Nov 5, 2013 at 10:27
  • @lonesomeday or most versions of IE (shudder) Commented Nov 5, 2013 at 10:27
  • What does div or jQuery(div) return in the console during the doRestlyle call? Commented Nov 5, 2013 at 10:28

2 Answers 2

3

"Any explanation?"

Apparently when you call your function the argument you pass in (that becomes div) is not a string with the appropriate selector or a reference to the DOM element in question (or a jQuery object containing a reference to that element).

You would need to call your function like this:

doRestyle("#idName");

Or with some variable that has been set appropriately:

var id = "idName";
doRestyle("#" + id);

Or perhaps in an event handler:

$("#idName").click(function() {
    doRestyle(this);
});

Etc.

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

Comments

0
function doRestyle(someID) {
        jQuery('#'+someID).css({
            "margin-top": addableMargin + "px",
            "margin-bottom": addableMargin + "px",
        });
}

:D

Comments

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.