Trouble calling a function

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Randwulf
Forum Commoner
Posts: 63
Joined: Wed Jan 07, 2009 7:07 am

Trouble calling a function

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Trouble calling a function

Post 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.
stratbeans
Forum Newbie
Posts: 12
Joined: Sat Aug 29, 2009 2:23 am

Re: Trouble calling a function

Post 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>
Randwulf
Forum Commoner
Posts: 63
Joined: Wed Jan 07, 2009 7:07 am

Re: Trouble calling a function

Post 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.
Post Reply