jsfiddle question [duplicate]
Asked Answered
F

4

6

I can't get this extremely simple jsfiddle to work. Its just supposed to alert test when button is clicked. What am I missing here?

http://jsfiddle.net/u9nG6/2/

Fannie answered 25/7, 2011 at 23:21 Comment(1)
Simple remark: Instead of bind click, you've better to trap submit event!July
C
8

You have to change the load method to no wrap (head).

It has something to do with how the JavaScript gets loaded and when the method signature is read. http://jsfiddle.net/u9nG6/11/

Coquelicot answered 25/7, 2011 at 23:27 Comment(1)
To be specific: the onDomReady wraps the function and it only exists in that scope. The no wrap means that the function exists in the global scope.Autotomize
C
2

See here.

You needed to define your validateForm function at the global scope in order to be able to use it in the HTML like that. Otherwise you had it defined as a function within the scope of the onDomReady event, which is inaccessible outside that scope.

A more "jQuery-ish" approach would be to use jQuery to handle the click event like this:

$("#id_btnSubmit").click(validateForm);

See here for an example of that suggestion.

Canaanite answered 25/7, 2011 at 23:29 Comment(0)
F
2

jQuery framework is selected to load onDomReady so your function is wrapped in the jQuery anonymous function $(function(){ }); and is not visible. Either change jQuery to load as no wrap (head) or define your function at the global scope.

From answered 25/7, 2011 at 23:29 Comment(0)
F
0

You're using jquery + ondomready. This means any javascript you write is placed within

$(function() {

and

});

this leads to a scope problem. You can try the following instead.

window.validateForm = function() {
}
Foldboat answered 25/7, 2011 at 23:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.