Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
How to extract the returne value of my function JavaScript

I used this function to get the ratio width/height of a photo to assign automatic values of portrait or lanscape:

    var newimage = new Image();
    newimage.src = ImageDir; //Directory of my image
    newimage.onload = function ratio()
{
    var width = this.naturalWidth;
    var height = this.naturalHeight;
    var divi = (width/height);
    return divi.value;
}
alert(divi)

Will return the correct value, but I want to in corporate this code:

if (divi > 1) {
        node.getElementById("myImage"+"-"+nump).dataset.size = ("1600x1200");
    } else {
        node.getElementById("myImage"+"-"+nump).dataset.size = ("1200x1600");
}

I tried someting like this:

var newimage = new Image();
    newimage.src = ImageDir; //Directory of my image
    newimage.onload = function ratio()
{
    var width = this.naturalWidth;
    var height = this.naturalHeight;
    var divi = (width/height);
    if (divi > 1) {
        node.getElementById("myImage"+"-"+nump).dataset.size = ("1600x1200"); //nump is the number of repetitions the While loop as been in (for increments)
    } else {
        node.getElementById("myImage"+"-"+nump).dataset.size = ("1200x1600");
}
}

But the values of if and else won't output... They do if I put them out of the first function. This is all in a While loop, so the divi variable will change everytime.

Can someone help me get that Divi value for my If condition? Thank you!

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!