views:

226

answers:

4

Is this the correct way to call my javascript function with jquery? I have spent the last 5 hours trying to figure out why this won't work.

<form class="rpt" id="rpt" action="">


$(function() {
  $("#rpt").submit(doSave);
});
</script>


Here is the small snippet of code in the javascript wysiwyg editor. I'm still using the exact same form tag in my html.

  // Save
  case "Save":
      WYSIWYG.updateTextArea(n);
      var form = WYSIWYG_Core.findParentNode("FORM", this.getEditor(n));
      if(form == null) {
       alert("Can not submit the content, because no form element found.");
       return;
      }
      form.submit();
  break;
A: 

It's even simpler than that:

$("#rpt").submit(doSave);

If that doesn't work, it's because you work with a JQuery set, even though you are interested in submitting the form element. If so, try convincing JQuery that it's only a single element:

$("#rpt").eq(0).submit(doSave);
PatrikAkerstrand
Hey machine, thanks for the help. I just tried both pieces of code but instead of showing me the alert inside that function, I am being directed to a blank white page. I don't understand this.. I'm about half nuts over this. :)
There is no change from the original code I posted.
Could you show us your function definition for doSave?
PatrikAkerstrand
Hi machine. I actually put the function inside of html between acript tages and still can't get this thing to fire. $(".rpt").submit(doSave);function doSave(){alert();}
+1  A: 

Since "rpt" is the name of the class and the name of the form, you could use "." or "#" and it will work assuming "doSave" is the name of a function to be used as a callback. So, it can be called like this:

$(".rpt").submit(doSave);
function doSave(){
  //do something
}

or you can have the function itself can be the parameter:

$(".rpt").submit(function(){
  //do something here
});

EDIT: If you don't want the form to postback. Add a "return false;".

$("#rpt").submit(doSave);
function doSave(){
  //do something
  alert("form submit fired");
  return false;
}
Jose Basilio
Hi Jose, thanks again for the help. This is really strange because I have taken your code and instead of relying on the inline js tag where I have the doSave function, I have placed the doSave function directly into the html between the script tags and I STILL can't get the alert to fire. Here is what I am using.$(".rpt").submit(doSave);function doSave(){alert();}
@nutjob - the alert() needs to have something or it will not fire. Ex: alert("some message");
Jose Basilio
Thanks Jose. I have tried it every which way. I just tried it now with alert('dfadfdadf'); and still nothing at all. :/ Thanks for the hand Jose.
In that case, we need to see more of your code.
Jose Basilio
Thanks for the edit Jose. You won't believe this but it doesn't work. I still get the same exact white screen after I submit the form. No alert at all. This is really weird. I also checked that the id name "rpt" wasn't being used elsewhere and it isn't, only in the form tag.
+1  A: 
Paolo Bergantino
I'm curious. Why does your post begin with "Hey, Frank"?
ichiban
Hey Paolo... Thank GOD you showed up. lol.. This thing is making me crazy! Something sooo simple and I have spent half of my Sunday trying to get it working and it STILL isn't. Anyhow.. Thanks so much for the help. Let me ask you.. I have a wysiwyg editor that uses form.submit(). This is tied to a little icon on the editor. After reading what you posted (and thank you for the link btw) which one do I need?
Because we know each other. :P
You can call me Frank too if you want. :)
@nutjob: What does the editor do on form.submit and what do you want to do on submit?
Paolo Bergantino
Ok, I **think** the form.submit simply fires whatever action="" is is the form tag. At least that's how it was working before. What I'd like is to have is your code simply echo back to the user whether the submission was successful or not, like on the other forms that you helped me with. This one is just different because of the javascript submit.form(). I don't know how to get it to work. If it were a regular input button, no sweat.
Maybe you need to piece together an actual example of what you're trying to do. A WYSIWYG editor should not really be messing around with the form's submit event - all it does is presentational - are you assuming that something happens on the submit event? When you say "all it does is fire whatever is action" that is what the browser is doing by default, so you can just bind an event to the form's submit event and do your processing there...
Paolo Bergantino
Maybe I should just mention this as a refresher but I want to use the query string that's generated and pass that to my php script then have php return a value for json and finally echo the output in the form of a div to the user. I don't need a url for the form action. At least that's not how I have been doing it anyhow. :)
Ok Paolo. I will post more code up top in my original post. Just for the record.. The way I had this working before was that I had a url in the form action like in your example and when I pressed the editor's save icon, the browser went to that url. I;ll post the code now. Yes, to answer your question, I am under the impression that when I press the icon that the form's action will execute. Its only because that was how it was working before.
Paolo, is there any way that I can use firebug to see what is happening when I press this stupid icon? :)
Frank: Looking at the code, you want to use the jQuery ajax code I've shown you in previous question INSTEAD OF form.submit(); - all that line of code is doing is submitting the form the plain old HTML way. You don't want to do that , you want to make an AJAX request. So instead of doing form.submit(); you would do the whole getting the url, data, serialize, etc. and create your $.ajax() request there.
Paolo Bergantino
Ok, great... I'm glad that you can see what's happening here. :) The code that I posted above came from the editor's code. Not sure I'd want to change it because I'm using that editor elsewhere in the app. I'm fairly certain that's not what you meant anyhow. The JQ way is how I would like to go. I just figured that I needed to use the editor's way of doing the submit because its tied to that cute little button. lol. Seriously though, if I could still use JQ and keep the button to submit the form, it'd be awsome. When you say that I would get the URL, data, etc... I still need to submit, no?
SO cut me off, too many words. Anyhow, I'd still have to submit the form using the icon on the editor, right?
Don't get hung up on the icon :) Although the code you posted is not really enough to be able to 100% tell you, if you put an alert before the form.submit(); and you get that alert when you click on the button you're talking about, all you have to do is create your own jQuery function to catch the submit event, return false, and do the AJAX stuff. I'm trying to teach you how to fish instead of giving you food, but if you want I can actually whip it up if you show me the editor you are using...
Paolo Bergantino
Excellent news Paolo... I do get the alert when I edit the editor's source. I added the alert to the case stmt and got the alert come up. Cool.. Ok, the URL for the editor is: http://www.openwebware.com some people don't like it but I have never found an html editor that was nearly this fast. If you think you can show me how to do this, I'm certainly open to it. I kinda feel like I'm a burden sometimes though when I'm sure you have better things to do than to teach me how to crawl. :)
Paolo, I'm floored man.. Wow. So you *did* have some hurdles to get around. I've never seen anything like it before. No matter what I tried wouldn't work at all. Now, I'd certainly like to learn a thing or two about this so if you wouldn't mind a question or two.. Now, only curosity here but wasn't there a way to use the doSave() function to get around all of that? Honestly, at the javascript level I am currently at, I'm certain that there is no way that I would have been able to figure that one out... Paolo, thank you again!
Also, can you recomment a decent html editor that is quick loading that isn't too bloated and bogged down with useless features?
The more popular two are fckeditor (http://www.fckeditor.net/) and TinyMCE (http://tinymce.moxiecode.com/) - the hurdles weren't horrible, but I certainly wouldn't have expected someone that's new to js/jquery to be able to catch them, so this time you were justified in being a little lost :) Not sure what you mean about the doSave function, but the way I did it above is the best way to get around it, in my opinion
Paolo Bergantino
A: 

It's possible that the doSave function must be defined earlier in source than the code binding it to the submit event...

This code does NOT work

// doSave is undefined at this point
$("#rpt").submit(doSave);

// Now we've defined doSave, but it's too late.
function doSave(){
  //do something
  alert("form submit fired");
  return false;
}

I believe this code ought to work:

$("#rpt").submit( function (){
    alert("submitted");
    return false;    
});

Or if you must define doSave separately, try this:

// First define doSave
function doSave(){
  //do something
  alert("form submit fired");
  return false;
}

// Then bind it
$("#rpt").submit(doSave);
Triptych
http://jsbin.com/isoya defines the function after it binds it and it works fine for me on FF at least
Paolo Bergantino
... and on IE ...
Paolo Bergantino
Well, I am grasping at ANYTHING and I tried all three pieces of code above and still nothing. I'm making the assumption that $("#rpt").submit(doSave); should bind the id rpt and call the function. right?
*Shrugs* I gave it a shot. My new opinion is that we need to see more code.
Triptych