0

I have the following inside of a larger function

var margin = $(document).ready(function(){
            marginReturn = $('.indbx'+n).css("margin")
            return marginReturn
})

and it is unable to grab my margin value. I have obviously tried many methods including plain

var margin = $('.indbx' + n).css('margin')
console.log(margin)

I've been able to get the value in a separate function but why create a new function just to grab a css property? the furthest I've gotten is my first example returning a [document] value

the below DOES return the value when called within a document.ready() function. even though all of my other attempts have been called from a document ready function.

function bxMargin(){
    var marginR = $('.indbx'+n).css("margin")
    return marginR
}

what am I doing wrong?

edit: https://codepen.io/anon/pen/RLdBRK?editors=1111

MalyG
  • 301
  • 1
  • 6
  • 15
  • Obvious question, is the element on the DOM when you want to grab the value? – Mouser Oct 20 '17 at 17:20
  • 2
    *"what am I doing wrong?"* You are misunderstanding how `$(document).ready()` works. It does not return the return value of callback. It couldn't because the callback is executed *when the DOM is ready*, not when `$(document).ready()` is called. https://learn.jquery.com/using-jquery-core/document-ready/ . Either restructure your code so that everything that needs to access `margin` is inside or called from the callback, or put the code after the DOM element you want to query so that you don't need `$(document).ready()` in the first place. – Felix Kling Oct 20 '17 at 17:21
  • Try adding `console.log($('.indbx'+n)[0])` to know if the DOM element really exists. – RainingChain Oct 20 '17 at 17:21
  • I'm not sure 'margin' will return which value 'margin-right' or else. So far I can remember that you should mention side as well which margin value you want to get something like that css("margin-left") or css("marginLeft"). Because you trying to get computed style value so far I got. – Hanif Oct 20 '17 at 18:09

0 Answers0