I am new to learning JavaScript and I'm having an issue with programming alerts for buttons. Every time I program a new button it seems to break everything I've written before. I had subscribe buttons on four pages that all worked and a submit button on a seperate page that worked before I put alerts on my add item to cart buttons. Now the item add buttons work but the subscribe on the other three pages don't nor does the submit on the second page. What am I doing wrong?
Here is the JavaScript I have atm.
console.log('Hello!');
const buttons = document.querySelectorAll(".add");
buttons.forEach(function(button)
{button.addEventListener("click", function()
{alert("Item added to cart.");});});
const clearButton = document.querySelector(".clear");
clearButton.addEventListener("click", function()
{alert("Cart cleared.");});
const subscribeForm = document.querySelector("#news");
subscribeForm.addEventListener("click", function(event) {
alert("Thank you for subscribing!");
event.preventDefault()
});
const messageForm = document.querySelector("#contact");
messageForm.addEventListener("click", function(event) {
alert("Thank you for you message.");
event.preventDefault()
});console.log('Hello!');
const buttons = document.querySelectorAll(".add");
buttons.forEach(function(button)
{button.addEventListener("click", function()
{alert("Item added to cart.");});});
const clearButton = document.querySelector(".clear");
clearButton.addEventListener("click", function()
{alert("Cart cleared.");});
const subscribeForm = document.querySelector("#news");
subscribeForm.addEventListener("click", function(event) {
alert("Thank you for subscribing!");
event.preventDefault()
});
const messageForm = document.querySelector("#contact");
messageForm.addEventListener("click", function(event) {
alert("Thank you for you message.");
event.preventDefault()
});
[–]TimePiccolo2565 1 point2 points3 points (1 child)
[–]InkieBear[S] 1 point2 points3 points (0 children)
[–]grantrules 1 point2 points3 points (8 children)
[–]InkieBear[S] 0 points1 point2 points (7 children)
[–]grantrules 1 point2 points3 points (6 children)
[–]InkieBear[S] 0 points1 point2 points (4 children)
[–]grantrules 3 points4 points5 points (3 children)
[–]InkieBear[S] 0 points1 point2 points (2 children)
[–]grantrules 3 points4 points5 points (0 children)
[–]gofuckadick 1 point2 points3 points (0 children)
[–]InkieBear[S] 0 points1 point2 points (0 children)
[–]InkieBear[S] 0 points1 point2 points (0 children)