1

This is what I usually see:

$("button").click(function(){
    $.post("demo_test.asp", function(data, status){
        alert("Data: " + data + "\nStatus: " + status);
    });
}); 

But I want to do this and it isn't really working:

<input type="submit" class='button_red' value="Create Pool" id="button" onclick="CreatePool()"><br />

function CreatePool(){
        $.post("demo_test.asp", function(data, status){
            alert("Data: " + data + "\nStatus: " + status);
        });
}

Why not and shouldn't it? I'm getting the impression post will only work inside an event listener... but the above are just two ways of saying the same thing correct?

1
  • what is the error in console? Commented Jul 5, 2015 at 23:14

1 Answer 1

2

I suppose the error is Uncaught ReferenceError: CreatePool is not defined Try to declare the function before using-it.

<script>
    function CreatePool(){
        $.post("demo_test.asp", function(data, status){
            alert("Data: " + data + "\nStatus: " + status);
        });
}
</script>

<input type="submit" class='button_red' value="Create Pool" id="button" onclick="CreatePool()"><br />

Check this DEMO

Sign up to request clarification or add additional context in comments.

4 Comments

Or possible use predefiined variable that already declared, $ for example: jsfiddle.net/3u0vyz2h
@stdob-- yah, i forgot this. +1 for hint
Jeez... this could really be the problem... too bad I deleted all the function code is a spat of anger, lol. It is good though, it seems like sometimes it has to be at the end and sometimes it has to be at the beginning, unintuitive.... But I suspect now, that this is a false impression.... functions need to be declared before they are used, right? I probably got the false impression because, at one point, I had java includes in the middle of the page making me think function X had to be at the end. I'll definitely try this when I get home.
To avoid situation like this use something like this $("selector").on("click", function); or $("selector").on("click", function(){ ....});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.