button onclick

Hi,

I have a button : <button id='test' type='button'>Test !</button>. If the button is clicked the following function is executed :

[php]function test() {
alert(“This is a test!”);
}[/php]

But if I place this : <script> document.getElementById("test").addEventListener("click", test); </script>

before the button it doesn’t work. It only works when I place if after the button. Is this normal ?

The button has to load before it will work, this is how I do javascript/jquery -> I put all that just before the tag. That way you are almost assured in getting the right results.

Typical standard is for an JavaScript on the page to be placed just before the closing body tag as Strider stated. If you were to add and

window.load

Clause, it should work though. Unless I have a very simple page, I separate each language into it’s own file and call it when needed however.

ok thanks.

Sponsor our Newsletter | Privacy Policy | Terms of Service