2

I have the following function:

 var emptyFields = false;

 function checkNotEmpty(tblName, columns, className){

    emptyFields = false;

     $("."+className+"").each(function() {
          if($.trim($(this).val()) === "" && $(this).is(":visible")){
             emptyFields = true;
             return false; // break out of the each-loop
          }
     });

      if (emptyFields) {
             alert("Please fill in current row before adding a new one.")
       } else {
              AddRow_OnButtonClick(tblName,columns);
       }
}

Its checking that all elements in the table are not empty before adding a new row, and I only need to check that the last row of the table has at least an element which its not empty and not the whole table.

The className its applied to the table. Please notice, that the problem I have its checking the last row and only one element of the row has to have some text in it. (e.g. each row has 5 textboxes at least one textbox need to have some text inside in order to be able to add another row, otherwise, the alert comes up).

2
  • Could you consider posting your HTML snippet defining at least the table and one table row? That would help a lot. Commented May 5, 2010 at 14:22
  • I am revising the code, If I can't find what is wrong I will post it. Thanks to all of you, I would give you lots of votes to all of you if I could. Once again, thanks. Commented May 5, 2010 at 15:19

3 Answers 3

2

This should work:

if ($('.' + className + ' tr:last input:text:visible[value]').length > 0) {
    AddRow_OnButtonClick(tblName,columns);
} 
else {
    alert("Please fill in current row before adding a new one.")
}

This of course assumes the className you're using is used on the table element.

Working example:

<script type="text/javascript">
$(function(){
    $('#btn').click(function(){
        if ($('.test tr:last input:text:visible[value]').length > 0) {
            alert('SUCCESS - at least 1 value is filled in!');
        } 
        else {
            alert("FAIL - all textboxes on last row are empty.")
        }
    });
});
</script>

<table class="test" border="1">
<tr>
<td><input type="text" value="filled" /></td>
<td><input type="text" value="in" /></td>
</tr>
<tr>
<td><input type="text" value="" /></td>
<td><input type="text" value="" /></td>
</tr>
</table>

<input id="btn" type="button" value="Click Me!" />
Sign up to request clarification or add additional context in comments.

6 Comments

@John Rasch:Thanks! I really hoped this works, but even if all fields are filled in in the last row, the alert comes up. Also, it only checks for text boxes, Would I have to make an else if statement to check for other elements shuch textareas, selectboxes?Thanks a lot.
@John Rasch: Darm! I does the same with the className, I can not create a new row, the alert comes up even if all fields have data in it. Thanks a lot for trying.
@Cesar - this definitely works for a test example I've written, which I will include shortly.
@John Rasch: You are right, this works, I am looking what could it be wrong on my code. I can see that you only look for input:text, how could I capture any element (e.g. textboxes, textareas, selectboxes, and others shuch radio and check boxes if possible)? Thanks.
@Cesar - that's a little bit more complicated, but I believe you can do that using the selector '.test tr:last :input[value]:not(:checkbox):not(:radio),.test tr:last :input[value]:checked'
|
1

Have you considered the :last selector and some children in addition to "."+classname? Something along the lines of $("."+classname+" tr:last td") should check each cell in the last row.

EDIT: Initially suggested last-child, but switching to something that fits the OP's needs better on comment.

7 Comments

To check the last row I added the following:$("."+className+":last").each(function() and work. I now only need to check that any of the elements has text and not all elements on the last row, thanks.
@Cesar Lopez - How about setting a variable outside of the closure that you set to true if one's .text() is non-empty and non-null?
@just: Sorry I did not solve getting to check the last tr, I was checking the last td instead. I also tried the solution :last-child td but it does not work. Thanks.
@Cesar Lopez - try $("."+className+"tr:last td").each(...)
@Justkt: I tried that but it does not work either, I dont know what I am doing wrong? Thanks.
|
1

There is no need for a loop:

if($("." + className + ":visible[value]").length) {
   // one or more has been filled out, and is visible
   // [value] matches non-empty values only
}

3 Comments

@karim79:I tried your if statement but it does not seem to work, also when you say one or more values has been filled out, how do I check this? Thanks.
@Cesar Lopez - That if statement returns the number of visible elements that have a non-empty value, so if it evaluates to true, then add another row, otherwise alert('Please fill in...
That would check all elements on the table, I think, I need to check only the elements from the last row of the table, would that be possible? Thanks a lot.

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.