1

One of the most powerful things of jQuery is to select any item on a page like this:

$(".block").css("border","3px solid red");

How can I also get the values of the selected items like width height and top left offset?

4
  • I need to iterate more than one element by the way ? Commented May 28, 2009 at 16:29
  • 2
    For iterating, you can use jQuery's "each." docs.jquery.com/Core/each#callback Commented May 28, 2009 at 16:33
  • The question title doesn't make sense ("How can return..."), despite having been edited. How do I edit the question title? (Or do I not yet have a high enough reputation score to do it?) Commented May 28, 2009 at 16:43
  • @Nosredna, you need a reputation >= 2,000 to edit non-wiki questions/answers. See the FAQ here: stackoverflow.com/questions/130654/… Commented May 28, 2009 at 17:32

4 Answers 4

2

Use width, height and position or offset:

var elem = $(".block");
var elemWidth = elem.width();
var elemHeight = elem.height();
var elemPosition = elem.position();
var elemOffset = elem.offset();

For further questions, first take a look into the jQuery documentation.

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

Comments

1
var height = $(".block").css("height");
alert(height);

Comments

0

Try this:

$(".block").css("border","3px solid red").offset().top
$(".block").css("border","3px solid red").offset().left

CSS/offset() in JQuery

Comments

0

Like @Gumbo said yes you can get the height and position. If you are interested in other css attributes you get the value like this:

var border = $(".block").css("border")

But for height and width you are better off using height() and width() functions

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.