0

So far i have the code below:

$('.pagination').each(function(){
    var paginationWidth = $(this).width();
    var pixelOffset = '-' + paginationWidth + 'px';
    console.log(paginationWidth.css('margin-left', pixelOffset ));
});

Console log shows "Object 57 has no method 'css'", the number being the width. Why is this the case on an each element? If its not an array why can't .css() be called upon each of said elements?

2
  • 4
    var paginationWidth = $(this).width(); which is not a jquery object (just a number). Try $(this).css('margin-left', pixelOffset ) Commented Dec 30, 2013 at 13:46
  • D'oh! Thanks for the quick response. Commented Dec 30, 2013 at 13:50

4 Answers 4

2

The issue here is nothing to do with the each loop. The .width() jQuery method returns a value, and you're then trying to set a CSS property of that value. The $(this) inside your each loop is still the object.

Your last line wants to be this instead.

$(this).css('margin-left', pixelOffset )
Sign up to request clarification or add additional context in comments.

Comments

1

Try this,

$('.pagination').each(function(){
    var page=$(this);
    var paginationWidth = page.width();
    var pixelOffset = '-' + paginationWidth + 'px';
    console.log(page.css('margin-left', pixelOffset ));
});

$(this).width() is not returns an object.It returns an integer number which representing it's width.

Comments

1

I think what you want to do is $(this)... literally if you excuse the joke... See below.

('.pagination').each(function(){
   var paginationWidth = $(this).width();
   var pixelOffset = '-' + paginationWidth + 'px';
   console.log($(this).css('margin-left', pixelOffset ));
);

Comments

0

jQuery works like

$(selector).function/event(.....

but paginationWidth is not a selector. selectors are objects to be selected.

Better to use

$('any-selector-to apply-margin-left').css('margin-left', pixelOffset );

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.