samedi 28 mars 2020

Send message with ID of checked checkbox from popup.js to background.js

I want to send an ID of the checked checkbox from popup.js to background.js. I tried it on the following way, but failed:

popup.html

<form id="cbx">
<label for="cb1">G</label>
<input name="cb1" type="checkbox">
<input type="submit" value="Submit">

popup.js

document.getElementById('cbx').addEventListener(

    'submit', function checkForm(event) {

    event.preventDefault();

    //Define the form element
    var form = document.getElementById("cbx");

    if (form.cb1.checked) {

chrome.runtime.sendMessage({greeting: "cb1"});

    }

    return true;

});

background.js

chrome.extension.onMessage.addListener(function (request, sender, sendResponseParam) {
if( request.greeting === "cb1" ){
    console.log("cb1"); 
}
})

After checking of the checkbox console remains empty. How can i handle this correctly?




Aucun commentaire:

Enregistrer un commentaire