why the getElementById() method isn't working right

I´m learning, so I want to make a button do something, for now, just show a message with the alert function. But it isn’t working, I think the method getElementById is not working and I don’t know why.

here is the javascript code:

function button(){
let btn = document.getElementById("sub");
btn.addEventListener('click',()=>{
    alert("clicked");
});
}

And I’m calling the function in the html file, with the onclick property, here is the code:

<button class="btn" id="sub" onclick="button()">Submit</button> 

105 thoughts on “why the getElementById() method isn't working right”

  1. pharmacie de nuit autour de moi pharmacie ouverte autour de moi , pharmacie aix en provence saint jerome act therapy hooks , Comprar Paroxetine genГ©rico, DГіnde comprar Paroxetine 20 mg. Comprar Paroxetine sin receta Comprar Paroxetine genГ©rico [url=https://kit.co/letlibiz/donde-comprar-paroxetine-20-mg-comprar-paroxetine-generico#]Paroxetine en farmacia Colombia[/url] act therapy summary Barato Mobic en Miami, Comprar Mobic medicamento. Mobic en farmacia Miami Comprar Mobic genГ©rico [url=https://kit.co/progecflub/comprar-mobic-medicamento-barato-mobic-en-miami#]Comprar Mobic sin receta[/url] , pharmacie auchan lac pharmacie de garde yvetot aujourd’hui , pharmacie de garde aujourd’hui neuilly sur seine. pharmacie ouverte orthez pharmacie de garde marseille jour ferie Comprar Nizoral genГ©rico, Comprar Nizoral medicamento. Nizoral en farmacia PerГє Comprar Nizoral genГ©rico [url=https://kit.co/huangfasma/comprar-nizoral-generico-donde-comprar-nizoral-200-mg#]DГіnde comprar Nizoral 200 mg[/url] , pharmacie auchan aubiere. pharmacie lafayette amiens tel, pharmacie uchaud pharmacie annecy loverchy . therapie cognitivo comportementale origine, therapie cognitivo comportementale yvelines therapie quantique definition pharmacie lafayette occitane auch .

    Reply
  2. Today, while I was at work, my sister stole my iPad and tested to see
    if it can survive a 30 foot drop, just so she can be a youtube sensation. My apple ipad is now destroyed
    and she has 83 views. I know this is entirely off topic but I had to share it with someone!

    Reply
  3. The other day, while I was at work, my cousin stole my iphone and tested to see if it can survive a thirty foot drop, just so she can be a youtube sensation. My apple ipad is
    now broken and she has 83 views. I know this is entirely off topic but I had to share it with someone!

    Reply
  4. Woah! I’m really digging the template/theme of this website.
    It’s simple, yet effective. A lot of times it’s tough to get that “perfect balance” between usability and visual appeal.

    I must say you have done a great job with this.
    Also, the blog loads very fast for me on Internet explorer.

    Exceptional Blog!

    Reply
  5. Hey there! Quick question that’s entirely off topic.

    Do you know how to make your site mobile friendly? My website looks weird when browsing from my iphone 4.

    I’m trying to find a theme or plugin that might be able to correct this
    problem. If you have any suggestions, please share. Thank you!

    Reply

Leave a Comment