Suresh Ponnukalai
Suresh Ponnukalai

Reputation: 13988

Simple Javascript function not working in jsfiddle

I have the following HTML Code.

 <select onChange="TestCall()">
<option>Text1</option>
<option>Text2</option>
<option>Text3</option>
<option>Text4</option>
</select>

My JavaScript have a simple alert message like below.

 function TestCall()
{
 alert("Test");
}

Check the following Jsfiddle. I have added my script in the javascript area of jsfiddle. It is not working.

JSFIDDLE NOT WORKING HERE

But If i move script block inside the HTML area of jsfiddle is working. Check the below one.

JSFIDDLE WORKING HERE

What wrong in my code? Any Idea?

Upvotes: 0

Views: 190

Answers (2)

VisioN
VisioN

Reputation: 145388

By default JSFiddle wraps the JS block into onLoad function:

... leading to something like:

window.onload = function() {
    function TestCall() {
        alert("Test");
    }
};

This means that your TestCall method is getting out of global scope and is not accessible from HTML attributes.

Change the option to No wrap - in <head> or No wrap - in <body> and it will work.

DEMO: http://jsfiddle.net/Q9ukX/3/

Upvotes: 4

Albzi
Albzi

Reputation: 15609

Fiddle

It works if you change the settings down the side.

Change it from onLoad to No Wrap - in body or No Wrap - in head.

Upvotes: 2

Related Questions