Reputation: 123
I have a function, which should check if input:text has value or if checkbox is checked. if not then popup appeares. It works only with checked/unchecked input, and not with input:text. What am I doing wrong here:
$("#wcv-product-edit #save_button").click(function(){
var checked = $("#shipping input:checked" ).length > 0;
var text_value = $("#shipping input:text" ).length > 0;
var store_checked = $("#store_shipping input:checked").length > 0;
if (!checked && !store_checked || !text_value && !store_checked){
$('#ship_wrong_popup').show();
$('.page .wcv-tabs .tabs-nav li a.shipping').addClass('ship_highlight');
return false;
} else if (!checked && store_checked || !text_value && store_checked){
// $('#ship_wrong_popup').show();
return true;
} else if (checked && store_checked || text_value && store_checked){
// $('#ship_wrong_popup').show();
return true;
}
});
Upvotes: 0
Views: 414
Reputation: 21672
The problem is this line:
var text_value = $("#shipping input:text" ).length > 0;
This is not only selecting "non-empty" textboxes, it is selecting any input of type="text"
regardless of whether or not it is empty. As long as a textbox exists within #shipping
, this will never be 0.
If you only have one textbox, check the length
of the value instead of the element count:
var text_value = $("#shipping input:text" ).val().length > 0;
If you have more than one textbox and want to see if all of them have content, you can filter out empty inputs from the element count by using .filter
:
var text_value = $('#shipping input:text').filter(function () {
return !!this.value;
}).length;
Upvotes: 1