V
V
Vladislav Chernushevich2014-10-01 11:28:43
JavaScript
Vladislav Chernushevich, 2014-10-01 11:28:43

How to select elements in jQuery?

Let's say we have this markup

<div class='a'>
<ul>
<li>высота1</li>
<li>высота2</li>
<li>высота3</li>
</ul>
</div>
<div class='a'>
<ul>
<li>высота4</li>
<li>высота5</li>
<li>высота6</li>
</ul>
</div>


The height of all li elements is different. The task is to select an element from the list with a maximum height and set this height to the rest of the elements.
I do so
var A = $('.a li'), max = 0, elem;
    A.each(function() {
        if (this.offsetWidth > max)
            max = this.offsetWidth, elem = this;
    });

But in this case, the maximum element is found for two lists at once. And I need to have a selection for each list separately. Is it possible to implement this if the block classes in which these lists are located are the same?

Answer the question

In order to leave comments, you need to log in

5 answer(s)
E
Evgeny Petrov, 2014-10-01
@Uladzislau

$('.a').each(function () {
  var max = 0;

  $(this).find('li')
    .each(function () {
      if (this.offsetHeight > max) {
        max = this.offsetHeight;
      }
    })
    .css({
      height: max + 'px'
    });
});

Is display:flex not suitable for this task ?

K
Konstantin Velichko, 2014-10-01
@Zoxon

var maxHeight = 0;
$('li').each(function() {
  var $this = $(this);

  $this.each(function() {
    var height = $(this).height();

    if (height > maxHeight) {
      maxHeight = height;
    }
  });

});
$('li').height(maxHeight);

I need to check if it works, everything seems to be correct.

S
Sergey Romanov, 2014-10-01
@Serhioromano

Maybe he messed up something there, wrote without checking. But it's like the idea itself is that you need to do 2 cycles.

$('.a').each(function(){
    var max = 0;
    $('li', this).each(function(){
        if (this.offsetWidth > max)
            max = this.offsetWidth
    })
});

F
FeNUMe, 2016-10-17
@FeNUMe

jQuery is not needed here, bare javascript will suffice
new URLSearchParams(location.search).get('v')

A
Adam Sulumov, 2017-01-15
@Ceekjee

Is it possible to make the code work more optimized?
Here I wrote.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question