Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pass data from multiple function returns to single variable in js/jquery

I have a function, which gets paragraph height, and calculates image height by subtracting 100vh – paragraph height, every time image is loaded.

It results in not satisfying effect, I would like to substract the highest value that this function get.

So what I’m thinking the best approach would be to pass data from multiple loads to single variable, and pick the highest one. Although I’m js beginner and have no idea how to do that. Correct me if I’m wrong.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$(".module img").one("load", function () {
    
    var z = $(this).next('p').height()
    console.log(z)
    $('.module img').css({'height': 'calc(100vh - ' + z + 'px)'})

}).each(function() {
  if(this.complete) {
      //$(this).load(); // For jQuery < 3.0 
       $(this).trigger('load'); // For jQuery >= 3.0 
  }
});

>Solution :

Sth like this should do the job:

It’s simply storing highest value in variable and use it

let maxHeight = 0;

$(".module img").one("load", function () {
    
    var z = $(this).next('p').height()

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

    console.log(z)
    $('.module img').css({'height': 'calc(100vh - ' + maxHeight + 'px)'})

}).each(function() {
  if(this.complete) {
      //$(this).load(); // For jQuery < 3.0 
       $(this).trigger('load'); // For jQuery >= 3.0 
  }
});

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading