JQuery class selector and 'this' link

Here is the problem: I need to select the elements on the page using the css class, and then set their width = parentElement.width - 1.
Therefore, the code looks like this: $j('.innerCellElement').width(this.parentElement.clientWidth - 1);
When I say this, I mean the current selector element. However, it is not interpreted as I want. I can do loops here, but I want to know if there is an elegant way to solve this problem.

+3
source share
1 answer

If you use jQuery ≥1.4.1, the method.width() can accept the function as input:

$('.innerCellElement').width(function(){ return this.parentElement.clientWidth - 1; });

otherwise loop over the collection.

$('.innerCellElement').each(function(){
  $(this).width(this.parentElement.clientWidth - 1);
});
+2
source

Source: https://habr.com/ru/post/1759640/


All Articles