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

92
Views
JQuery passing parameters to custom function not working properly

Good day all,

I need some help figuring this jquery puzzle. spent quite sometime trying to get this working without luck. i need to be able to pass parameters to my custom function so that they are not persistent and can be overwritten. currently it will keep the firs set of parameters passed even though I passing new ones or nothing.

here is the code for the custom function that i attached to $

$(document).ready(() => {
  $.addImage = function addImage(params) {
    'use strinct';

    const ops = params || undefined;

    let cropper;

    let avatar = document.getElementById('Avatar');

    console.log(ops);
    // 1st call is $.addImage({ isKey1: 'yes' }) => { isKey1: 'yes' } (good)
    // 2nd call $.addImage() => { isKey1: 'yes' } nope should be undefined

    // 1st call is $.addImage() => undefined (good)
    // 2nd call $.addImage({ isKey1: 'yes' }) => undefined (nope should be the object)

    // we are in certification mode, change the image sources
    if (ops && ops.isKey1 === 'yes') {
      avatar = document.getElementById(rowImgID);
    }

    // eslint-disable-next-line prefer-arrow-callback
    document.getElementById('imgSave').addEventListener('click', function () {
      let canvas;

      if (cropper) {
        // eslint-disable-next-line prefer-arrow-callback
        canvas.toBlob(function (blob) {
          let url;

          console.log(ops);
          // 1st call is $.addImage({ isKey1: 'yes' }) => { isKey1: 'yes' } good
          // 2nd call $.addImage() => { isKey1: 'yes' } nope should be undefined or null

          // 1st call is $.addImage() => undefined (good)
          // 2nd call $.addImage({ isKey1: 'yes' }) => undefined (nope should be the object)

          if (ops && ops.isKey1 === 'yes') {
            url = 'url1';
          } else {
            url = 'url2';
          }

          console.log('url:', url);

          // $.ajax({
          //   // here send blob
          //   // send other values
          //   blob,
          //   .....
          //     .....
          // })
        });
      }
    });
  };
});

and when i try to call the function twice for example

first call without param like this

  $('#btnAddAvatar').on('click', (e) => {
    e.preventDefault();
    $.addImage();
  });

and second call somewhere else in the code like this

if ($target.hasClass('addImg')) {
  const options = {
    isKey1: 'yes',
    otherKey: otherValue,
  };
  $.addImage(options);
}

in the second call the "params" object always default to the value of the first call. so in this case "params" is null therefore ops becomes null. if I call $.addImage(options) first and then call $.addImage(), the second call will have all the options when I do not want the options.

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!