LittleBobbyTables LittleBobbyTables - 5 months ago 6
Javascript Question

addEventListener calls the function without me even asking it to

So we have a page:

<span id='container'>
<a href='#' id='first'>First Link</a>
<a href='#' id='second'>Second Link</a>
</span>


And want to add some click events:

first.addEventListener('click', function(){alert('sup!');})


Works like a charm! However, when you make the second argument an external function:

function message_me(m_text){
alert(m_text)
}

second.addEventListener('click', message_me('shazam'))


It calls the function immediately. How can I stop this? So annoying!

Here's a live demo: http://jsfiddle.net/ey7pB/1/

Answer
function message_me(m_text){
    alert(m_text)
} 

second.addEventListener('click', 
    function() {
        message_me('shazam');
    }
);

Here's an updated fiddle.