I want to ask if there's a better way in jQuery to select multiple text input then check if any of them has a value. Here's my code:
if ($("#reference").val() != "" || $("#pin").val() != "" || $("#fName").val() != "" || $("#mName").val() != "" || $("#datepicker").val() != "") { /*logic goes here */ }
To check if the input text box is empty using jQuery, you can use the . val() method. It returns the value of a form element and undefined on an empty collection.
Just use: $("input:empty"). length == 0; If it's zero, none are empty.
You could do like below:
if ($("#reference,#pin,#fName,#mName,#datepicker").filter(function() { return $(this).val(); }).length > 0) { //.. }
Using a common function like the following would make it reusable:
function hasValue(elem) { return $(elem).filter(function() { return $(this).val(); }).length > 0; }
And you could call it like this:
hasValue("#my-input-id");
Try jQuery each()
$('input[type=text]').each(function(){ var text_value=$(this).val(); if(text_value!='') { console.log('Value exist'); } })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With