views:

1001

answers:

5

Ok, because I am such a noob and have no idea how javascript works... My thinking was flawed. I am halfway there now. I have taken one of the poster's suggestions and set up a function and then in the html form tag, I call it. This works. I thought that I needed to have an id selector to have JQuery trigger but that isn't the case. I'm sorry. I know I am not making any sense at all but please bear with me, I'm trying. :)

Now that I have the form pointing to the javascript/jquery script. I now need to fix the jquery. Here is the code.

function doSave(){
  $(function() {
    $('.error').hide();
    $("#dfar").click(function() {
      $('.error').hide();

    var textArea = $('#dfarReport');
    if (textArea.val() == "") {
    alert('sd');
      textArea.show();
      textArea.focus();
    return false;
    }

For some reason, even though I press the submit button and have included the id selector of the text area I cannot see the alert. Can someone please tell me what I am doing wrong?

+1  A: 

You could get the textarea by

$('textarea')

If it's the only one on your page.

You just need to call submit on the form that the textarea is contained within

EDIT:

After reading your update, you just want to validate that the textarea is not blank before submitting the form. You just need to set up the validation

var textArea = $('#results'); //Get the textarea

if (textArea.val() == "") {
    textArea.show();
    textArea.focus();
return false;
}
// perform the form submit here as textarea is not blank

Could you post the whole object where you have got the code beginning with

 case "Save":

from? It would probably be a good idea to use that object to perform the submit.

EDIT 2:

In regard to your comment about the page being refreshed, submitting the form causes the page to be POSTed. It sounds like you want to POST data using AJAX.

Take a look at jQuery's $.ajax() command

Russ Cam
It is the only one on my page. So I don't really have to use the "submit" button id then?
Hi Russ, so you mean the javascript editor? You want me to post that code? Its really long. I tryed your code and I think it is working but the problem I'm having is that the page is being refreshed. Is there a way to stop this?
submitting the form causes the page to be POSTed. It sounds like you want to post data using AJAX. Take a look ta jQuery's $.ajax() documentation http://docs.jquery.com/Ajax/jQuery.ajax#options
Russ Cam
Yes Russ, I want ajax to handle the post data. I already have that in the script but it won't work. I still have a problem with the selectors because I;m not making it past the if condition. I can't see the alert box come up when I don't fill in the textarea.
In your original code, the id of the textarea was 'results' - have you tried the jQuery selector using that id?
Russ Cam
Hi Russ, yes, I changed that from results to what the selector actually is. Can't remember now what exactly it was because I don't have the code in front of me. It still won't work though. :(
Ok I looked at the example I posted above and the selector is there. I changed it rome results to dfarReport.
+1  A: 

Change button to

<script>
function doSave()
{
   // save data using jQuery
}
</script>
<input type="button" value="Save" onclick="doSave(); return false;">

or

You can change the form with

<form onsubmit="doSave(); return false;">
Pavels
Hi Pavels. The problem is that the javascript submit is not accessable. I will post the code above from the editor so you can see what I mean.
Pavels, thanks. I am almost there now using your suggestion. :)
A: 

You could do something like

$(document).ready(function(){
    $("#submit_btn").click(function(){
        mySubmit($("#myForm")) ; // call your existing javascript submit routine
    });
});

or if you prefer,

$(document).ready(function(){
    $("#myForm").submit(function(){
        mySubmit($("#myForm")) ; // call your existing javascript submit routine
    });
});
Stobor
Ahaa.. I think you may be right... I should be able to use the id from the form tag.. brb..
Hi Stobor, it doesn't work. I reposted the code I am working with above. Can you tell me what's wrong?
+2  A: 

ok, I think you'll need this:

$(document).ready(function(){

    $("form").submit(function(){
        var textArea = $('#dfarReport');

        if (textArea.val() == "") {
            textArea.show();
            textArea.focus();
            return false;
        }
        else // you'll need to add the else and the return true
        {
            return true; 
        }
    });

});

you need to return true when the form is valid.

More info: http://docs.jquery.com/Events/submit

But make sure that you validate the data on the server as well!

Natrium
Hi Natrium, unfortunately, that doesn't work. I placed an alert after f (textArea.val() == "") { and didn't fill anything in but I didn't see the alert come up which tells me that something is still not right. Would you know why?
and does the alert show when you put the alert BEFORE the if ? $("form").submit might be incorrect. So you can check that selector as well.
Natrium
you changed your code in your first post.var textArea = $('#dfarReport');That's not what it said first. That might be the problem.
Natrium
A: 

please post the code you use.

You edited like 3x and there's always a different approach.

Ok, if you want to use the code as you defined in your first post right now, you 'll have to do this:

function doSave(){
  $(function() {
    $('.error').hide();
    $("#dfar").click(function() {
      $('.error').hide();

    var textArea = $('#dfarReport');
    if (textArea.val() == "") 
    {
      alert('sd');
      textArea.show();
      textArea.focus();
    return false;
    }
    else
    {
        return true;
    }
Natrium