Two functions in the same javascript onclick event

Asked

Viewed 73 times

2

I have two functions that need to interact in the same DIV.

# ambientEdit(this);

Her job is to keeping DIV pressed for 3 seconds, open the screen of confirm() javascript, passing the ID.

# survey Start(this);

This function, when clicking only once in the DIV is forwarded to another screen passing some parameters.

Trying:

<div class="lista fade" onclick="ambienteEdit(this);vistoriaIniciar(this)">

vistoriaIniciar(), works, but the ambienteEdit() does not work. If I remove the vistoriaIniciar() the function ambienteEdit() works.

Question:

How to have both events in the same DIV?

Thank you.

function ambienteEdit(object) {
   let aID     = object.getElementsByClassName('aID')[0].value;

   let delay;
   document.addEventListener("mousedown", function() {
     delay = setTimeout(_ => confirm("Dialogo"), 3000)
   });

   document.addEventListener("mouseup", function() {
     clearTimeout(delay);
   });

   console.log(aID)
}



function vistoriaIniciar(object) {
  let aID     = object.getElementsByClassName('aID')[0].value;
  let aCodigo = object.getElementsByClassName('aCodigo')[0].value;
  let aNome   = object.getElementsByClassName('aNome')[0].value;

  window.location='vistoriaIniciar.html?codigo='+aCodigo+'&ambiente='+aNome+'&id_ambiente='+aID;
}
<div class="lista fade" onclick="ambienteEdit(this);vistoriaIniciar(this)">
  BLA BLA BLA
</div>

  • Error appears in CONSOLE?

  • @Paulohdsousa No mistake, just does not execute the ambienteEdit.

  • Why do you want the system to wait 3 seconds to open a confirmation?

  • Take a look here: https://stackoverflow.com/a/52751982/11890088

  • @Danielzazula because it’s something I’m doing for cell phone. In the same DIV the person passes to another screen, but if the person keeps pressed will open the edit screen.

  • 1

    Deliberately adding a Document.addeventlistener to every click is wrong, both in your question and in the answers, this is not how you use the listening events, it may even work, but it is not correct and records a number of things that are no longer being used, consuming what does nothing after the first addition.

Show 1 more comment

2 answers

2


The event onclick will only be fired if you click and drop. With this, the document.addEventListener("mousedown"... will only be created if it happens at least once.

Call the function ambienteEdit in an event attribute onmousedown in the div:

<div class="lista fade" onclick="vistoriaIniciar(this)" onmousedown="ambienteEdit(this)">

And the function will be like this:

function ambienteEdit(object) {
   let aID     = object.getElementsByClassName('aID')[0].value;

   let delay = setTimeout(_ => confirm("Dialogo"), 3000)

   document.addEventListener("mouseup", function() {
     clearTimeout(delay);
   });

   console.log(aID)
}

1

You are making a redirect in the method vistoriaIniciar()

window.location='vistoriaIniciar.html?codigo='+aCodigo+'&ambiente='+aNome+'&id_ambiente='+aID;

And is waiting 3 seconds to run confirm in the method ambienteEdit()

What happens is that the redirect is happening before 3 seconds, so it seems that your method was not called, but it is simply waiting for the 3 seconds.

That’s why it works when you take vistoriaIniciar(), because there is no redirect.

To resolve do the following, move your method ambienteEdit for the page LOAD and remove it from the onclick of the div, it will look like this.

<script>

    window.onload = function load() {
         var div =  document.getElementById("onPressDiv");
        ambienteEdit(div);
    }

function ambienteEdit(object) {
   let aID     = object.getElementsByClassName('aID')[0].value;

   let delay;
   document.addEventListener("mousedown", function() {
     delay = setTimeout(_ => confirm("Dialogo"), 3000)
   });

   document.addEventListener("mouseup", function() {
     clearTimeout(delay);
   });

   console.log(aID)
}



function vistoriaIniciar(object) {
  let aID     = object.getElementsByClassName('aID')[0].value;
  let aCodigo = object.getElementsByClassName('aCodigo')[0].value;
  let aNome   = object.getElementsByClassName('aNome')[0].value;

  window.location='vistoriaIniciar.html?codigo='+aCodigo+'&ambiente='+aNome+'&id_ambiente='+aID;
}
   </script>

And your div.

<div class="lista fade" id="onPressDiv" onclick="vistoriaIniciar(this)">
            BLA BLA BLA
        </div>
  • What would be the solution?

  • @Thiago I don’t know what you want to do, I would separate the methods, redirect into one element and open confirm into another

  • If you click once on the DIV, you go to another screen. If you click and hold for 3 seconds, you open the confirm.

  • @Thiago tries this way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.