TransWikia.com

onclick is not calling the javascript function

Stack Overflow Asked by Umakanth Pendyala on December 15, 2020

Guys I have written a code like this in the script tag of my web page

window.reset = function (e, elementId) {
  e.wrap("<form>").closest("form").get(0).reset();
  $(elementId).attr("src", " ").width(30).height(30);
  e.unwrap();
};

window.a = function (e, elementId) {
  e.wrap("<form>").closest("form").get(0).reset();
  $(elementId).attr("src", " ").width(30).height(30);
  e.unwrap();
};

Both of the functions do the same work. But the problem is the reset function is not called on click but the ‘a’ function is called

What I meant is:

This is the button that calls the reset function. This way the reset is not being called

 <button type="button" id="img-refresh" class="btn btn-secondary" data-dismiss="modal" 
    onclick="reset($('#image'), '#img-show');"
  > Refresh </button>

This way the ‘a’ method is being called.

 <button type="button" id="img-refresh" class="btn btn-secondary" data-dismiss="modal" 
    onclick="reset($('#image'), '#img-show');"
  > Refresh </button>

When I replace ‘reset’ with ‘a’ it is working.
Can someone tell me what is the problem?

Even though I change it like this

window.customFunction = {
  reset: function (e, elementId) {
    e.wrap("<form>").closest("form").get(0).reset();
    $(elementId).attr("src", " ").width(30).height(30);
    e.unwrap();
  },
};

and use call it like this, It is not working

 <button type="button" id="img-refresh" class="btn btn-secondary" data-dismiss="modal" 
    onclick="customFunction.reset($('#image'), '#img-show');"
  > Refresh </button>

One Answer

My guess is that reset is a reserved word the window context... or maybe an other script from your vendros are overwriting your function

My first question is... why are you declaring your functions like that ?

I can suggest 2 options :

You could use a simple function declaration instead of the window.reset stuff you did

  function reset() { 
    // do stuff
  }

And then use it exactly the same way you already did ?

OR

If you expressly want it in the window context, use a parent key to ensure you did not overwrite other functions nor any extarnal script overwrite yours...

window.myCustonFunction = {
  reset: function() {  },
  a: function() { }
}

Answered by Patrick Ferreira on December 15, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP