onclick='' is not working when trying a Chrome extension

huangapple go评论57阅读模式
英文:

onclick='' is not working when trying a Chrome extension

问题

I am trying to make a Chrome extension however onclick='' is not working when used due to inline javascript below is the code I use:

let createTasks = () => {
    TaskContainer.innerHTML = `<span class='TextMyTask'>My Tasks</span>`
        data.map((x,y) => {
            TaskContainer.innerHTML +=`
            <div id=task${y} class='secondtaskcontainer'>
                <span class='tasktextclass'>${y+1}${'-'}${x.text}</span></br>
                <span class='datetextclass'>${'To do on date : '}${x.thedate}</span></br>
                <span class='descriptiontextclass'>${'This task consists to : '}${x.description}</span></br>
                <span class='urgencytextclass'>${'This task is : '}${x.urgency}</span>
                <i class="Edit" id='edit' onclick='Edittask(this)'>&#x270E;</i>
                <i class="Trash" id='trash' onclick='Removetask(this); createTasks()'>&#128465;</i>
            </div>`
        })
    clearform()  
    savedata()  
}
function Removetask(e){ 
    Taskid = e.parentElement.id
    let removedTask =document.getElementById('Taskid')
    const lastChar = Taskid.at(-1);
    e.parentElement.remove(removedTask)
    data.splice(lastChar,1)
    savedata()
}

function Edittask(e){
        openpopup()
        let selectededitedTaskid = e.parentElement.id
        const lastChareditedtask = selectededitedTaskid.at(-1)
        InputTask.value = data[lastChareditedtask].text
        InputDate.value = data[lastChareditedtask].thedate
        Description.value = data[lastChareditedtask].description
        Urgency.value = data[lastChareditedtask].urgency
        Removetask(e)
        savedata()
}

I tried to remove the onclick='' and replacing it with addEventlistener using
var Edit = document.getElementById('edit') and Edit.addEventListener('click', Edittask()) but then I get the error message 'Cannot read properties of null' as element is not yet created. May somebody help me solve this problem?

英文:

I am trying to make a Chrome extension however onclick='' is not working when used due to inline javascript below is the code I use:

let createTasks = () =&gt; {
    TaskContainer.innerHTML = `&lt;span class=&#39;TextMyTask&#39;&gt;My Tasks&lt;/span&gt;`
        data.map((x,y) =&gt; {
            TaskContainer.innerHTML +=`
            &lt;div id=task${y} class=&#39;secondtaskcontainer&#39;&gt;
                &lt;span class=&#39;tasktextclass&#39;&gt;${y+1}${&#39;-&#39;}${x.text}&lt;/span&gt;&lt;/br&gt;
                &lt;span class=&#39;datetextclass&#39;&gt;${&#39;To do on date : &#39;}${x.thedate}&lt;/span&gt;&lt;/br&gt;
                &lt;span class=&#39;descriptiontextclass&#39;&gt;${&#39;This task consists to : &#39;}${x.description}&lt;/span&gt;&lt;/br&gt;
                &lt;span class=&#39;urgencytextclass&#39;&gt;${&#39;This task is : &#39;}${x.urgency}&lt;/span&gt;
                &lt;i class=&quot;Edit&quot; id=&#39;edit&#39; onclick=&#39;Edittask(this)&#39;&gt;&amp;#x270E;&lt;/i&gt;
                &lt;i  class=&quot;Trash&quot; id=&#39;trash&#39; onclick=&#39;Removetask(this); createTasks()&#39;&gt;&amp;#128465;&lt;/i&gt;
            &lt;/div&gt;`
        })
    clearform()  
    savedata()  
}
function Removetask(e){ 
    Taskid = e.parentElement.id
    let removedTask =document.getElementById(&#39;Taskid&#39;)
    const lastChar = Taskid.at(-1);
    e.parentElement.remove(removedTask)
    data.splice(lastChar,1)
    savedata()
}

function Edittask(e){
        openpopup()
        let selectededitedTaskid = e.parentElement.id
        const lastChareditedtask = selectededitedTaskid.at(-1)
        InputTask.value = data[lastChareditedtask].text
        InputDate.value = data[lastChareditedtask].thedate
        Description.value = data[lastChareditedtask].description
        Urgency.value = data[lastChareditedtask].urgency
        Removetask(e)
        savedata()
}

I tried to remove the onclick='' and replacing it with addEventlistener using
var Edit = document.getElementById('edit') and Edit.addEventListener('click', Edittask()) but then I get the error message 'Cannot read properties of null' as element is not yet created. May somebody help me solve this problem?

答案1

得分: 1

你遇到的问题是在addEventListener方法上,你立即调用了Edittask函数,而不是将其分配为事件侦听器。你需要传递函数引用而不带括号。下面是修正后的代码:

let createTasks = () => {
    TaskContainer.innerHTML = `<span class='TextMyTask'>My Tasks</span>`;
    data.map((x, y) => {
        TaskContainer.innerHTML +=`
        <div id=task${y} class='secondtaskcontainer'>
            <span class='tasktextclass'>${y+1}-${x.text}</span></br>
            <span class='datetextclass'>To do on date : ${x.thedate}</span></br>
            <span class='descriptiontextclass'>This task consists to : ${x.description}</span></br>
            <span class='urgencytextclass'>This task is : ${x.urgency}</span>
            <i class="Edit" id='edit'>&#x270E;</i>
            <i class="Trash" id='trash'>&#128465;</i>
        </div>`;
    });

    clearform();
    savedata();

    // 在创建元素并将其附加到DOM后,添加事件侦听器
    const editButtons = document.getElementsByClassName('Edit');
    for (let i = 0; i < editButtons.length; i++) {
        editButtons[i].addEventListener('click', Edittask);
    }

    const trashButtons = document.getElementsByClassName('Trash');
    for (let i = 0; i < trashButtons.length; i++) {
        trashButtons[i].addEventListener('click', function() {
            Removetask(this);
            createTasks();
        });
    }
};

// 你的其余代码...

在更新的代码中,在创建元素并将其附加到DOM后,我们使用addEventListener为编辑和删除按钮添加事件侦听器。我们使用getElementsByClassName选择按钮,并循环遍历集合,以将事件侦听器分别附加到每个按钮。请注意,在删除按钮的情况下,我们使用匿名函数在单击按钮时调用RemovetaskcreateTasks函数。

请确保从HTML代码中删除onclick属性,因为我们在程序中动态附加事件侦听器。

英文:
The issue you&#39;re facing with the `addEventListener` approach is that you&#39;re invoking the `Edittask` function immediately instead of assigning it as the event listener. You need to pass the function reference without the parentheses. Here&#39;s the corrected code:
let createTasks = () =&gt; {
    TaskContainer.innerHTML = `&lt;span class=&#39;TextMyTask&#39;&gt;My Tasks&lt;/span&gt;`;
    data.map((x, y) =&gt; {
        TaskContainer.innerHTML +=`
        &lt;div id=task${y} class=&#39;secondtaskcontainer&#39;&gt;
            &lt;span class=&#39;tasktextclass&#39;&gt;${y+1}${&#39;-&#39;}${x.text}&lt;/span&gt;&lt;/br&gt;
            &lt;span class=&#39;datetextclass&#39;&gt;${&#39;To do on date : &#39;}${x.thedate}&lt;/span&gt;&lt;/br&gt;
            &lt;span class=&#39;descriptiontextclass&#39;&gt;${&#39;This task consists to : &#39;}${x.description}&lt;/span&gt;&lt;/br&gt;
            &lt;span class=&#39;urgencytextclass&#39;&gt;${&#39;This task is : &#39;}${x.urgency}&lt;/span&gt;
            &lt;i class=&quot;Edit&quot; id=&#39;edit&#39;&gt;&amp;#x270E;&lt;/i&gt;
            &lt;i class=&quot;Trash&quot; id=&#39;trash&#39;&gt;&amp;#128465;&lt;/i&gt;
        &lt;/div&gt;`;
    });

    clearform();
    savedata();

    // Add event listeners after elements are created
    const editButtons = document.getElementsByClassName(&#39;Edit&#39;);
    for (let i = 0; i &lt; editButtons.length; i++) {
        editButtons[i].addEventListener(&#39;click&#39;, Edittask);
    }

    const trashButtons = document.getElementsByClassName(&#39;Trash&#39;);
    for (let i = 0; i &lt; trashButtons.length; i++) {
        trashButtons[i].addEventListener(&#39;click&#39;, function() {
            Removetask(this);
            createTasks();
        });
    }
};

// Rest of your code...

In the updated code, after creating the elements and appending them to the DOM, we add event listeners to the edit and trash buttons using addEventListener. We select the buttons using getElementsByClassName and loop through the collection to attach the event listeners to each button individually. Note that in the case of the trash buttons, we're using an anonymous function to call both Removetask and createTasks functions when the button is clicked.

Make sure to remove the onclick attributes from the HTML code since we're attaching the event listeners programmatically.

huangapple
  • 本文由 发表于 2023年6月12日 05:54:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定