Home >Web Front-end >JS Tutorial >Why Isn't My JavaScript Working in JSFiddle?

Why Isn't My JavaScript Working in JSFiddle?

Barbara Streisand
Barbara StreisandOriginal
2024-12-21 14:22:10820browse

Why Isn't My JavaScript Working in JSFiddle?

JSFiddle: JavaScript Not Working

A user has encountered an issue with their JavaScript code not functioning in JSFiddle. Despite following the documentation that indicates JS code should be added to the section and HTML code to the , clicking the button results in an error message stating "test not defined".

The Issue

Upon examination of the provided code, the problem lies in the JSFiddle wrapping setting. By default, it is set to "onLoad," which wraps all JavaScript within a function executed after the result has loaded. This creates a localized scope for the variables, rendering them inaccessible in the global scope.

The Solution

To resolve this issue, modify the wrapping setting to "no wrap." This ensures that the JavaScript code is not contained within a function and is available in the global scope.

Corrected Code

<input type="button" value="test" onclick="test()">
function test() { alert("test"); }

By implementing these changes, the JavaScript code will function as intended when the button is clicked.

The above is the detailed content of Why Isn't My JavaScript Working in JSFiddle?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn