-3

I have a function entitled changeSize() which i want to change the size of the div / span etc that it is used next to. Here is my code:

function changeSize(width, height) {
    $(this).css('height', height);
    $(this).css('width', width);
}

$('#square').changeSize(200, 200);

I presumed that the changeSize would replace (this) with ('#square') because it is used next to the function - but it doesn't

CMedina
  • 4,034
  • 3
  • 24
  • 39
j. hol
  • 21
  • 1
  • 1
  • 3
  • 6
    `changeSize` is just a function and has nothing to do with a jquery object – PeeHaa Mar 30 '16 at 16:55
  • 1
    "next to the function"? $(this) depends on the event scope – Sterling Archer Mar 30 '16 at 16:56
  • functions and methods don't work that way. It could work that way if you called it like this: `changeSize.call($('#square').get(0), 200, 200);`, but you should just extend jQuery. https://jsfiddle.net/obwLj7kq/ – Joseph Marikle Mar 30 '16 at 16:58

1 Answers1

4

Your changeSize function is not a method on the jQuery object. You would need to pass the element as a parameter.

function changeSize(element, width, height) {
  element.css('height', height);
  element.css('width', width);
}

changeSize($('#square'), 200, 200);
Guy
  • 10,931
  • 5
  • 36
  • 47