Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
95
rated 0 times [  97] [ 2]  / answers: 1 / hits: 9591  / 10 Years ago, tue, august 5, 2014, 12:00:00

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!


More From » jquery

 Answers
13

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.


[#43331] Monday, August 4, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
quinn

Total Points: 160
Total Questions: 86
Total Answers: 101

Location: Belarus
Member since Tue, Mar 14, 2023
1 Year ago
;