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!