Reputation: 1368
I have a page where i have a table it consists of a 3 textboxes inside each row, also i have a add row button at the bottom of the page, by clicking this should add a new row to the table, but it should trigger this action only if all the inputs in the table are not null. Anyway i have done a little bit but its not working fine for me. Can anybody take a look on my code and help me to sort out what is wrong with that?
What i have done in JQuery is given below
// Add functionality Scripts
$('#teacherAdd').live('click', function(event) {
if(IsValidTableContent()){
// my code to add new row
return false;
}else{
event.preventDefault();
}
});
function IsValidTableContent(){
var isvalid = false;
$('#adminList tbody tr td input:text').each(function(){
if($(this).val() == ''){
return isvalid;
}else{
isvalid = true;
return isvalid;
}
});
return isvalid;
}
also similarly Can you say how to iterate through a select box inside the table to check whether the first option is selected or not?
Upvotes: 0
Views: 2717
Reputation: 10179
Try removing the last return statement of your function.
Upvotes: 0
Reputation: 4213
Something like this should work (provided your selector is correct):
function IsValidTableContent(){
var isvalid = true;
$('#adminList tbody tr td input:text').each(function(){
if ($(this).val() === '') {
isvalid = false;
return false; // breaks out of each loop
}
});
return isvalid;
}
Alternatively:
function IsValidTableContent(){
return $('#adminList tbody tr td input:text').filter(function() { return $(this).val() === '';}).length === 0;
}
Upvotes: 5
Reputation: 1087
first thing I noticed is that you are setting isvalid to true if one is and then returning true for all the rest because you are not resetting it, just return true; or return false; and skip the variable setting.
EDIT
Ok, I see what you are trying to do, variable is backwards. you want to start out with true and if any are false set it to false and then it will return false. Currently if any are true it returns true even if the others are false.
Upvotes: 0