[jQuery] Binding a function to a variable (Newbie question)
Hi all,
I've just started using jquery, and as you can tell from my blog, very close to just having started javascript, but I'm determined to do things properly.
I've written a post function that, for the sake of argument, pops an alert when you submit a form.
$(document).ready(function(){
initialiseForm();
});
function initialiseForm() {
$("form#fPollX").bind("submit", post());
}
function post() {<br clear="all"> alert('hello');
return false;
}
This works fine. Now I want to abstract it out so that any form can (for this version anyway) return its own message, based (currently) on a switch in post(). Either or neither form or both may be on the page.
So now I've tried:
$(document).ready(function(){
initialiseForm("fPollX");
initialiseForm("fPollY");
});
function initialiseForm(formID) {
if (!document.getElementById) return false;
if(document.getElementById(formID)){
theForm = "form#" + formID;
$(theForm).bind("submit", post(formID));
}
}
function post(formName) {
switch(formName){
case "fPollX":
alert('hello X ' + formName);
return false;
break;
case "fPollY":
alert('hello Y ' + formName);
return false;
break;
default:
return false;
}
}
The problem with this, I think is the fact I'm trying to 'bind' a function to a variable using the $( ) function. In this case, the post() function isn't called, it just runs (
e.g. alerting "hello X fPollX") the correct case, if the form is on the page).
The question here is either: how do I bind a function to a submit event, if I don't know exactly what the form's going to be called in the function?
Many thanks in advance for your help,
All the best,
Dan.
--
Daniel Eastwell
Portfolio and articles:
<a href="http://www.thoughtballoon.co.uk">http://www.thoughtballoon.co.uk</a>
Blog:
<a href="http://www.thoughtballoon.co.uk/blog">http://www.thoughtballoon.co.uk/blog</a>
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/