1

STOP THE PRESS!

My fault... I tried to read properties which was not set at the time I wanted to read them!

I'm using children and each to loop through children elements of a DIV. This gives me a this object which I use to move the element so it becomes a child of another element. This works nice... but I also need to access the inline style "left" of the element to be moved... just cannot figure out how to get that value...

$jquery("#thediv").children(".someclass").each(function () {

// move the element to another div
$jquery("#right").append($jquery(this));

});

The above code works nicely... But before I move the element I need to get the inline left value for the element. I have tried:

$jquery(this).css("left"); 

The above line give me "auto" in return, the actual value for the left is "0px". What do I have to use to get the left value?

I also would like to set the inline style to empty before moving the element... For this I have tried:

$jquery(this).attr('style', '');

The above line did not empty the inline style, it is just as originally output for the element. What do I have to use to empty the inline style for this element?

The inline style for the elements that is represented by the variable this in the loop, have the following inline style: style="position: absolute; left: 0px; top: 0px;"

where left is either "0px" or e.g. "350px" and where the top is not set to "0px" for all elements (but not of importance here)...

I came across this post: jQuery .css("left") returns "auto" instead of actual value in Chrome

where I thought I had found a solution... $jquery(this).position().left; the problem is that this allways gives me back "0", which is not correct... since every second element should have left="350px" and not "0px"

Currently I have this jquery code just before the closing body tag ... maybe that has something to do with this issue... But I thought it should not be a reason since the styles I'm trying to access is all inline.

Community
  • 1
  • 1
  • 1
    try `.offset` or `.position` (see the API for the distinction). Also, what is `$jquery`? another alias for `$` and `jquery`? – Dave Oct 20 '13 at 20:58
  • I have tried both .offset and .position . .offset allways returns 0, and .position allways return 230.5 . None of them show the actual value as set in the inline style for the divs. $jquery is an alias for $. – user1742543 Oct 20 '13 at 22:27
  • It would be helpful if you could post a sample of your html or a jsfiddle. – spacebean Oct 20 '13 at 22:59
  • Reduce your question to *just the question* and post the solution as an *answer* to the question. When posting questions, though, it's best to post a complete question including (relevant minimal/[sscce](http://sscce.org/)) HTML and JavaScript/jQuery. And please, don't add 'solved' to your question title, accept an answer and let the system denote that it's been solved (if you want to accept your *own* answer, that's fine, though I believe there's a two-day wait before you're allowed to do so). – David Thomas Oct 20 '13 at 23:33

1 Answers1

0

Are you trying to get the width from #thediv or from .someclass? If you need the width from #thediv then you need to use parent: $(this).parent().css("left"); since the context of this within the each loop is the someclass div.

See example here: http://jsfiddle.net/thespacebean/NJ9ny/

Oh, and to remove inline CSS altogether you can use .removeAttr("style")

spacebean
  • 1,554
  • 1
  • 10
  • 13
  • I'm trying to get left property of the style attribute, of the children of the #thediv, that is, the children that is using the class .someclass . I need the left property value as it is set in the inline style for each of the div's. I have also tried using .removeAttr("style") and it did not remove anything... – user1742543 Oct 20 '13 at 22:16
  • It should work like it does in my example, so if you wouldn't mind posting the relevant portions of your html it might be easier to find the source of the problem. – spacebean Oct 20 '13 at 23:00
  • Sorry for this... I was trying to read a property that was not set when I was trying to access it. No wonder why this was not working... Anyway... thanks to those that tried to help me out :) – user1742543 Oct 20 '13 at 23:31