-1

i have a button and some inputs, i want to let jquery check for me if none of the inputs is empty before doing some action : so this is what i've done :

$('#my_button)').click(function() {
if(
for(i=0;i<$('#my _button').parent().prev().children().children().length;i++){){ //this is to check if the inputs exist
$('#my_button').parent().prev().children().children().eq(i).val()!==''; // i've tried .val().is(:empty); but not working too
//so the test fails for somehow ! and the if condition doesn't work
// google chrome developers inspector gives me this error (Uncaught SyntaxError: Unexpected token for)
}
){
//do some action
}}

and this is my jsfiddl test !

user1283226
  • 17
  • 1
  • 9

2 Answers2

0
<table>
 <tr>
  <td>
   <div><input id='i1'></div>
   <div><input id='i2'></div>  
  </td>
  <td><button id='my_button'>click me</button></td>
 </tr>
</table>

Script:

    $('#my_button').on('click', function() {
        var in1 = $('#i1').val();
        var in2 = $('#i2').val();

        if(in1 != '' && in2 != ''){
               // do something
            }
            else{
              //alert

            }
    }
Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
  • i must mention that i can't associate any ID with inputs because they are created dynamically ... and it will be a mess if i tried to includ ID's – user1283226 Jan 15 '13 at 15:44
  • and i don't want to disable my button ! i want do some other action ONLY IF all the inputs are empty – user1283226 Jan 15 '13 at 15:45
0

It gives you the unexpected token warning on the for because it is an unexpected token. You can't put for loops in the boolean expression of an if statement.

You will need to do something similar to this pseudo-code:

$('#my_button').click(function() {
  succeed = true;
  for all of my inputs {
    if(input is empty){
       succeed = false;
    }
  }
  if(succeed) //do stuff
}
Jesan Fafon
  • 2,234
  • 1
  • 25
  • 29