I've got a function that is working fine (well, almost fine):
function clickMe() {
var evt = new MouseEvent("click", {
bubbles: true,
cancelable: true,
view: window,
});
var buttonId = document.getElementById("fib_button")
h = buttonId.nextSibling;
h.className = (h.className == 'hidden') ? '' : 'hidden';
buttonId.innerHTML = (buttonId.innerHTML == 'Show') ? 'Hide' : 'Show';
}
document.getElementById("fib_button").addEventListener('click', clickMe);
Now, I want to make it more generic so that the ID name gets passed to the function so I don't have to hard code it within the body of the function but it does not work:
function clickMe(divID) {
var evt = new MouseEvent("click", {
bubbles: true,
cancelable: true,
view: window,
});
var buttonId = document.getElementById(divID)
h = buttonId.nextSibling;
h.className = (h.className == 'hidden') ? '' : 'hidden';
buttonId.innerHTML = (buttonId.innerHTML == 'Show') ? 'Hide' : 'Show';
}
document.getElementById("fib_button").addEventListener('click', clickMe("fib_button");
Another problem I have is that in this line:
buttonId.innerHTML = (buttonId.innerHTML == 'Show') ? 'Hide' : 'Show';
"Hide/Show" should be put the other way round but if I swap the two, it stops working correctly.
Please advise
Here's a fiddle https://jsfiddle.net/2b3b7kp5/14/
document.getElementById("fib_button").addEventListener('click', function() {clickMe("fib_button"); }). Here is Updated Fiddle