1

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/

3
  • 1
    Use anonymous function document.getElementById("fib_button").addEventListener('click', function() {clickMe("fib_button"); }). Here is Updated Fiddle Commented Oct 17, 2015 at 20:43
  • Thank you. It works. You seem to have tidied up my html which broke the code in terms of recognising nextSibling. It looks like the next sibling element needs to be in the same line: <button id="fib_button">Show</button><div> Commented Oct 17, 2015 at 20:53
  • @Wasteland Welcome. Glad to help Commented Oct 17, 2015 at 20:55

3 Answers 3

1

Keep in mind that the addEventListener function is waiting for a function and as such your clickMe function should return one. Inside the clickMe function you may then return an anonymous function that we'll have access to the the scope and whatever arguments you send to clickMe.

What you're looking for is something like this:

function clickMe(divID) {
    return function () {
        var evt = new MouseEvent("click", {
            bubbles: true,
            cancelable: true,
            view: window,
        });
        console.log(divID);
        var buttonId = document.getElementById(divID)
        h = buttonId.nextSibling;
        h.className = (h.className == 'hidden') ? '' : 'hidden';
        buttonId.innerHTML = (h.className == 'hidden') ? 'Show' : 'Hide';
    }
}

document.getElementById("fib_button").addEventListener('click', clickMe("fib_button"));
Sign up to request clarification or add additional context in comments.

1 Comment

clickMe("fib_button") inside the addEventListener would fire immediately on page load.
0

Use anonymous functions function () { return function() {...}} Updated example https://jsfiddle.net/2b3b7kp5/25/

Comments

0

to be more generic, it might be best to use this.

function clickMe() {  
    h = this.nextSibling;
    h.className = (h.className == 'hidden') ? '' : 'hidden';
    this.innerHTML = (this.innerHTML == 'Show') ? 'Hide' : 'Show';
}

document.getElementById("fib_button").addEventListener('click', clickMe);

then this would be the element which triggered the event.

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

if you swap then, you would be setting innerHTML to show if it says show, everytime. therefore it would never change.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.