简体   繁体   中英

Click function not working in JavaScript console?

I am trying to write a script because of which, when I click on one element in the list, all elements get clicked automatically.

But the thing is, I am trying to run it in the JS console.

And the console keeps throwing an exception that the click() function is not defined at anonymous (some address) .

I also tried the trigger('click') event. But that didn't work either. I am assuming the problem is with the old version of jQuery.

Any idea what can I do to get a click on-all-at-once? This is the code:

$(document).ready(function() {
  var e = document.getElementsByTagName("Button");
  for (i = 0; i < e.length; i++) {
    e.onclick = e.trigger("click");
  }
});

This is the code for the banned names:

<div class="container_element sub_element hover_element">
<div class="wrap_element">
<div class="element_name">
<p>Drut</p>
</div><div class="delete_element delete_banned">
<button value="Drut" type="button">
<i class="remove_element close_room remove_private fa fa-2x fa-close">
</i></button></div></div></div>

This is the screenshot of what the list looks like: https://ibb.co/T0cYVZ2

For those wondering why I am trying to do this: I am basically a chat admin. There are over 2k banned people. I can't manually clear all the bans one by one, it'll take centuries that's why I am forced to write this script. And no I don't have access to the DataBase or the hosting.

Since you are already using jQuery you could do:

var $buttons = $('button').click(function(){

     // do something when it's clicked

     // trigger click on the others also
     $buttons.not(this).click()
});

Not sure why you would need to do this...seems unusual

Add JQuery CDN as reference, enclose your $(document).ready() function with script tag and replace e.trigger("click") with console.log("click") like this -

hope it will help you.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
  $(document).ready(function() {
    var e = document.getElementsByTagName("Button");
    for (i = 0; i < e.length; i++) {
      e.onclick = console.log("click");
    }
  });
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM