Seems super basic, but allergies have reduced my IQ. The background toggle is fine. But, added event listener does nothing. Most likely has to do with passing the ID on click…
// Define functions
function funcChange(clicked_id) {
var el = (clicked_id);
var element = document.getElementById(el);
element.classList.toggle("bgred");
}
function sayHello() {
alert("Hello World!");
}
// Select button element
var btn = document.getElementById("clicked_id");
// Assign event listener to button
if(btn){
btn.addEventListener("click", sayHello);
}
I’m trying to have independence between each button/ID. So the three IDs you noted work because onclick="funcChange(this.id)" sends the ID to the function. That’s why the background color toggle is working fine. Now if I could get the other function to follow that.
OK, I’m going to translate that to mean I need to review syntax for correct use of getElementById()
Give me a little time to make revision and test it …
Thank you @bbsmooth !
@bbsmooth@skaparate Oh wait that’s not right! What I really need to do is find the proper way to capture clicked_id (again) and assign it to btn
Allergy brain-fog strikes again…
What a noob I am…
A) My overall intent has been to have two actions from one click. That’s why I tried to add an event listener.
B) The change in background color succeeds because onclick="funcChange(this.id)" in the HTML does send the ID but only to that particular function.
C) I’m now researching how to get the ID copied outside the function, so that it will be available to an event listener that will provide me a second action opportunity (in addition to the background change). Please let me know if I am on the wrong path?
I have so far believed that finding a way to move the variable holding the element ID out of the function would be the way to have a second action in the one click handler? All my other attempts have generated errors.
I got the client request in October, this was delayed for months. The web page will hold many product descriptions with photo, each entry full width. A box floated in one corner of ea prod description will say “More” Here is the spec:
A) Clicking on “More” must:
1-Change “More” to “Less” (toggle)
2-Change “More” background color (toggle)
3-Open a hidden text box with more product info above the existing text box. Above means previously in the DOM.
B) Page must hold as many opened product boxes as page visitor wishes.
C) Mobile friendly.
So, what you just showed me will undoubtedly work well. My problem is that I got sidetracked by the addEventHandler concept. Not trying to ignore you. Ea of the JS gurus here is The Man.
Progress. This works only on the first of three test buttons. Thereafter, clicking on the second button changes MORE to LESS on the first button. The actions are not constrained to the proper “02, 03” ID.
Here is the broken function, followed by link to example…
var el = (clicked_id);
var element = document.getElementById("mole");
element.classList.toggle('lessmore');
console.log(element);
console.log(el + " func mor");
}```
[more at replit...](https://replit.com/@OkieFrontend/Button-show-with-event-listener#script.js)
Be sure to have your browser’s JS console open when you click on the buttons. I think you’ll see an error message that you will find helpful. When the message is something like “element is null” it’s usually because you attempted to get an element from the DOM but none were found. I’d check the id you are using on line 10.
I was using the console in replit, but moved code to a local file.
Well, it’s now constrained to each element, so I may have been looking at a cached version before.
But definitely an error msg …
Thx
Wow, that was quick: The error is gone. I pulled down the getElementsByClassName solution I used in the second function, and modified it for the third function.
Plus, there was something goofy in the class name for the paragraph that hides and then shows.