0

I have put the code on jsfiddle.net. The problem I am having is that the function does not seem to get called when showToast button is clicked.

The actual code
<button type="button" onClick="showAndroidToast('Hello Android!')">Show Toast</button><br/>

function showAndroidToast(name){      
        alert("hi");
}

I got the error:

ReferenceError: Can't find variable: showAndroidToast;

Anyone help? Thanks!

sammiwei
  • 3,140
  • 9
  • 41
  • 53
  • You should put the relevant code directly inside your question, regardless of the jsfiddle link. That way if the link dies, the code is still here. – Thomas Eding Dec 22 '11 at 00:48
  • i am not able to find any buttons in above code!!! – buch11 Dec 22 '11 at 00:50
  • @buch11 I changed it. Sorry I had a old reference link of jsfiddle up – sammiwei Dec 22 '11 at 01:00
  • 1
    http://stackoverflow.com/questions/5431351/simple-example-doesnt-work-on-jsfiddle The reason behind is because it is handled differently. – drhanlau Dec 22 '11 at 01:18
  • sammiwei -- your js is fine (except check the capitalization of onclick). The problem is that you're using jsfiddle. @cherhan shows the issue in his comment. Moral of the story: real JS developers should not use jsfiddle or similar. – Larry K Dec 22 '11 at 03:24
  • Is the function defined inside of `script` tags? – Larry K Dec 22 '11 at 01:14
  • I tried both ways. external js, and within the script. Not working – sammiwei Dec 22 '11 at 01:21
  • correction: I tried inline. works. But I still want to incorporate that into an external js though
    ``
    – sammiwei Dec 22 '11 at 01:59
  • Mystery Issues. Today it worked. Dont know why.. – sammiwei Dec 22 '11 at 19:58

2 Answers2

1

The problem is that it is not onClick but onclick, and your function should be declared in some cases like this:

<script>
 window.showAndroidToast = function(){
 //code
};
</script>
<button type="button" onclick="window.showAndroidToast('Hello Android!')">
    Show Toast
</button>

This is just to be found globally, just to be sure it is not a problem with the browser itself.

khael
  • 2,600
  • 1
  • 15
  • 36
0

Remove type from your button tag:

<button onClick="showAndroidToast('Hello Android!')">Show Toast</button><br/>
//and check

Hope it helps

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162