0

`Hey I was making a function but it seems onclick function is not working in google chrome extension and showing me the error

Basically my function is used to click all the checkboxes when I click on the button through add event listener which i read in stackoverflow here is the link of that post addEventListener calls the function without me even asking it to

After building that the thing is not working not sure what to do next

HTML CODE:

!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="body.css">
    <title>Linkedin Extension</title>
</head>
<body>
<main>
    <div class="main">
        <H1 class="h1">Select All</H1>
        <p class="p1">This is to Select all the checkboxes</p>
        <button type="button" id="select-all" >Run me</button>
    </div>

</main>
<footer class="end">Made with Love</footer>
<script src="script.js"></script>
</body>
</html>

Javascript Code

document.addEventListener('DOMContentLoaded', function() {
    document.getElementById("select-all").addEventListener("click", selectAll);
  });

  function selectAll(){
    var inputs = document.getElementsByClassName("invitee-picker-connections-result-item__checkbox");
    for(var i=0; i<inputs.length;i++)
    {
    inputs[i].click();
    }
    }

Let me know how this will code ran when I click on the html button

Here is the screenshot attached what it will do when it will run enter image description here

I want to run this code in an extension when I will click the button`

  • You need to inject the function into the web page using chrome.scripting.executeScript. Also remove lines 1 and 3 in your script.js because it already runs after DOMContentLoaded. Note that the popup is a separate window so it has its own separate devtools: right-click inside the popup and select "inspect" in the menu. – wOxxOm Dec 06 '22 at 12:18
  • Sorry man new to this things have'nt done that before Still unable to do that – MOZZAM SHAHID Dec 08 '22 at 19:08
  • See [these examples](https://stackoverflow.com/a/67227376). – wOxxOm Dec 08 '22 at 19:54
  • `const tabId = getselectAll(); chrome.scripting.executeScript( { target: {tabId: selectAll()}, files: ['script.js'], }, () => { var btn = document.getElementById("select"); btn.addEventListener("click", selectAll); function selectAll(){ var inputs = document.getElementsByClassName("invitee-picker-connections-result-item__checkbox"); for(var i=0; i – MOZZAM SHAHID Dec 08 '22 at 19:55

0 Answers0