Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking if any textboxes contain text

I have a HTML structure like this:

<div class="input_div_0">
    <input type="text" class="textbox input">
</div>
<div class="input_div_1">
    <input type="text" class="textbox input">
</div>
<div class="input_div_2">
    <input type="text" class="textbox input">
</div>

I want to write an expression that will return true if any of the inputs contain text

EDIT

Based on Dimitar's answer I've come up with this:

function checkInputs()
{
    $('input.textbox').each(function () {
        if ($(this).val() !== "") {
            return true;
        }
    });
    return false;
}

if (!checkInputs()) {
        //Display message
    }

However the false is returned every time.

like image 904
Frayt Avatar asked Mar 16 '23 21:03

Frayt


1 Answers

Try utilizing Array.prototype.some

var res = Array.prototype.some.call(document.querySelectorAll("input")
          , function(el, i) {
              return el.value.length > 0
          });

console.log(res);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="input_div_0">
  <input type="text" class="textbox input">
</div>
<div class="input_div_1">
  <input type="text" class="textbox input">
</div>
<div class="input_div_2">
  <input type="text" class="textbox input" value="0">
</div>
like image 127
guest271314 Avatar answered Mar 23 '23 08:03

guest271314