6

I have an jQuery object with 3 members:

var elements = $("#" + this.wrapperName + ">ul>li>a>img");
Object { 0: <img>, 1: <img>, 2: <img>, length: 3, prevObject: Object, context: HTMLDocument → JSUmstellung, selector: "#newsdesk>ul>li>a>img" }

I want to change eachs CSS-Property, so I made this:

elements.each(function(index, element) {
    element.css({
        "width": elemWidth,
        "height": elemHeight,
        "top": elemTop,
        "left": elemLeft
    });
}

When I run this code, I just get the following error:

TypeError: element.css is not a function

What am I doing wrong? Do I have to access my element in another way?

Thanks in advance!

1
  • For what it's worth, if you're not going to use index or element, you don't have to include them: elements.each(function() {. Then use $(this) in the function. Commented Aug 5, 2014 at 14:23

3 Answers 3

19

The problem is that element is a DOM node, without access to jQuery methods, what you need to use is $(this) or $(element):

elements.each(function(index, element) {
    $(this).css({
        "width": elemWidth,
        "height": elemHeight,
        "top": elemTop,
        "left": elemLeft
    });
}

Or:

elements.each(function(index, element) {
    $(element).css({
        "width": elemWidth,
        "height": elemHeight,
        "top": elemTop,
        "left": elemLeft
    });
}

Or you could, instead, use cssText (if you really want to work with DOM nodes):

elements.each(function(index, element) {
    element.style.cssText = 'width: ' + elemWidth + '; height: ' + elemHeight + ' top: ' + elemTop + '; left: ' + elemLeft;
}

Bear in mind that, within each(function (index, element){ ... }); element is exactly the same as this (and, as a corollary, $(this) is the same as $(element)). I'm not sure quite what the benefit is, of using the element at all.

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

3 Comments

Ahh! Thanks, I thought jQuery did that automatically.. :D
A jQuery object is just a wrapping around the underlying DOM element. That wrapper comes with some overhead, though. To support the effort of being as fast as possible, jQuery rarely (never?) automatically wraps a DOM object. So the base DOM objects are the context (this) and what get passed in (element).
Re: advantage of using element - maybe if instead of using an anonymous function, you're calling an existing function that's in a different context. So using this in that context would not represent the DOM element.
1

You have to wrap it in the jquery format. Like so:

elements.each(function(index, element) {
    $(element).css({
        "width": elemWidth,
        "height": elemHeight,
        "top": elemTop,
        "left": elemLeft
    });
}

notice the:
$(element)

Comments

0

You have to use $(this) selector inside the each loop

elements.each(function(index, element) {
    $(this).css({
        "width": elemWidth,
        "height": elemHeight,
        "top": elemTop,
        "left": elemLeft
    });
}

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.