How to simulate [mouseup()] element by element using get()

Asked

Viewed 39 times

1

I have a web application which I have elements like input.

<input class="switch-input" name="0" type="checkbox" />
<input class="switch-input" name="1" type="checkbox" />
<input class="switch-input" name="2" type="checkbox" />
<input class="switch-input" name="3" type="checkbox" />

And I have a Javascript code that I need to set these inputs for FALSE and simulate a MOUSEUP for my application to perform an action based on this. Basically used:

$("input[type=checkbox]").prop("checked", false);
$("input").mouseup();

Which worked very well initially, but in this case runs everything together and now I need a little delay. I tried to do:

$("input[type=checkbox]").prop("checked", false);
for(i=0;i < $("input").length; i++)
{
    setTimeout($("input")[i].mouseup(), 100);
}

But it returns me this error in console:

off-proc-rec.js:29 Uncaught Typeerror: $(...)[i]. mouseup is not a Function

I also tried using:

 setTimeout($("input").eq(i).mouseup(), 100);

And I got this mistake:

VM811:1 Uncaught Syntaxerror: Unexpected Identifier

Does anyone have any idea how to do that?

  • Test as follows: https://jsfiddle.net/LkL4ne6a/

1 answer

2


One option is to get the references of inputs by javascript and, within the loop, call a function to uncheck by increasing the time between executions. Something like:

function go() {
  var inputs = document.querySelectorAll("input[type=checkbox]");
  var tempo = 100;
  for (var i = 0; i < inputs.length; i++) {
    var name = inputs[i].name;
    tempo = tempo + 500;
    desmarcar(name, tempo);
  }
}

function desmarcar(name, tempo){
    setTimeout(function() {
      $("input[name='"+name+"']").prop("checked", false);
      $("input[name='"+name+"']").mouseup();
    }, tempo);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class="switch-input" name="0" type="checkbox" />
<input class="switch-input" name="1" type="checkbox" />
<input class="switch-input" name="2" type="checkbox" />
<input class="switch-input" name="3" type="checkbox" />

<button onclick="go()">Testar</button>

Browser other questions tagged

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