views:

57

answers:

2

Basically what I'm trying to accomplish is I want to check multiple dynamic forms on a page. Then if All textareas are empty OR have the default value, perform a function. Otherwise if any of them are not empty without the default value, cancel the function.

I know how to get the total count of the divs/forms or go through each one, sorta. Just can't put it together. I want it to check them all first before doing anything.

if (jQuery("div[id^='statuscontainer_']:last").length == 0){

}

I don't think this would get quite what I need to do. Or either I not sure how to form it into the function I need. Thanks

+1  A: 

You could take this approach

  1. Give each input a similar class, in my example, inputBox
  2. Create a script to add the total of each inputBox length. If that total == 0, take whatever approach, otherwise continue with the script

    $('#show').click(function() {
        var total = 0;
        $('.inputBox').each(function() {
            total += this.value.length;
        });
        if (total == 0) {
            // Do whatever if it's empty
        } else {
            // Do whatever if its not
        }
    });
    
  3. ???
  4. Profit

Try it

http://jsfiddle.net/kLnHC/

Edit

Though, in your application, you'd probably want to change the event that triggers it to $('#yourForm').submit(function() { rather than a click function.

Robert
Thanks you very helpful. However,I guess I should have explained more. Its not a click nor submit. I do auto refreshing of a page. I just wanted to check to make sure the user isn't typing something in one of the forms and it do a refresh causing them to loose their data. So I have it on a timer. When the set interval fires, I want it to check the boxes, if its empty or not default value, do nothing else the function. I almost got it but its not quite working with my dyanmic forms. Can live() somehow work with this?
Pjack
BTW that is a very cool site. Awesome...
Pjack
actually it is iterating through with each but somehow its still returning false using this statement if(jQuery(this).val() != "Input Text" || jQuery(this).val() !=""){ ok = false;}
Pjack
Can you elaborate, You added that if statement to my function? If so, where?
Robert
I got it working. It's a bit different but I will accept your answer because that would work too.
Pjack
A: 

use total += $(this).val(); instead of total += this.value.length; will check if there is only a " " and/or a "\n" in your textarea

jUbO