-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 !

2 Answers 2

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

i got your idea .. any jsfiddle example ?
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

            }
    }

2 Comments

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
and i don't want to disable my button ! i want do some other action ONLY IF all the inputs are empty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.