Help with javascript

Status
This thread has been locked.

FelixDev

Typical Developer and Designer
Premium
Feedback score
9
Posts
1,113
Reactions
331
Resources
0
Code:
let kick = document.getElementById('kick_btn')

const post_method = () => {
    Axios.post('/kick')
}

kick.addEventListener('click', post_method)

So I am trying to add a click event on a button, But I am getting an error
Uncaught TypeError: Cannot read property 'addEventListener' of null
at post_button.js:7
But I don't understand why anyone got an idea?
 
PebbleHost
High performance, consistent uptime and fast support. Minecraft hosting that just works.

jodgi

Developer
Supreme
Feedback score
5
Posts
158
Reactions
36
Resources
0
Try putting it inside a window.onload function
Code:
window.onload=function() {
    let kick = document.getElementById('kick_btn')
    const post_method = () => {
        Axios.post('/kick');
    }
    kick.addEventListener('click', post_method)
};
or adding a null check
Code:
if(kick) {
    kick.addEventListener('click', post_method);
}
 
Last edited:
Status
This thread has been locked.
Top