Page 1 of 1

Trouble calling a function

Posted: Tue Sep 22, 2009 6:22 pm
by Randwulf
Hi,

I have this function in a js file separate from the main file:

Code: Select all

 
function close(keyid)
{
    alert("testing");
    document.getElementById(keyid).innerHTML = "";
}
 
And I try to call it from the main file with this button:

Code: Select all

 
<a onclick="close(2)">Click</a>
 
I can't get it to work! It works when I use

Code: Select all

<body onload="close(2)">
But I can't use a button to call it for some reason. All of the other functions in the separate file are able to be called by the onclick handler, but this one isn't and I have no clue why.

Re: Trouble calling a function

Posted: Tue Sep 22, 2009 7:06 pm
by califdon
You are using "2" as the ID of an HTML element??? I don't think that's even a valid element ID. You need to pass the ID (...ID='something'...) of the HTML element.

Re: Trouble calling a function

Posted: Wed Sep 23, 2009 1:42 am
by stratbeans
Please use href in place of onclick because you are using anchor tag..

This solution may be solved your problem..


<a href="javascript:close(2)";return false;" >Click</a>

Re: Trouble calling a function

Posted: Wed Sep 23, 2009 4:55 pm
by Randwulf
stratbeans wrote:Please use href in place of onclick because you are using anchor tag..

This solution may be solved your problem..


<a href="javascript:close(2)";return false;" >Click</a>
That worked, thanks a lot :D

Now I shall go to le PHP forum and pay it forward.