silverlight513 silverlight513 - 16 days ago 8
jQuery Question

Onclick function doesnt work

I tried creating this script and it works when I call the function oncall but when I create a button and use the onclick function the script doesn't run.

var i = 0;

function increase() {

var amount = parseInt($('.amount').text());

i = amount;
var random = Math.floor(Math.random() * amount);
var counter = window.setInterval(function () {
if (i == amount + random) {
window.clearInterval(counter);
}
document.getElementById('result').innerHTML = i++;
}, 10);
}


http://jsfiddle.net/7MLK5/2/

Answer

Your increase function is not in global scope, put it in global scope and it is fine.

In fiddler LHS panel, in the second dropdown onload is selected by default, this will make fiddler to add all the script inside a onload handler making them local in scope so when your inline onclick handler tries to look for the method in the global scope it will not find it there. An easy fix is to select No Wrap - In Body/Head in the dropdown - it will not wrap the contents in another function making them in global scope.

Demo: Fiddle

Also since you are using jQuery, I would prefer to add the event handlers using jQuery rather than using inline scripting

Comments