shantaram shantaram - 15 days ago 14
jQuery Question

jQuery : check if next file input is not empty (Selected)

I have 10

<input type='checkbox'>
Checkboxes and next to them I have 10
<input type='file'>
files

I have this HTML

<tr>
<td align="center">
<input name="imagecheck" type="checkbox" value="0" required="required">
</td>
<td align="left">
<label class="Label_Green">Photograph</label>
</td>
<td>
<input id=Photograph name=myFile type="file">
</td>
<td colspan="1"></td>
</tr>


I want to check, depends upon checkbox checked, I want user to select file also.

For this I want to check next() file input value, but I'm not able to get, what should I used

So far I tried this, I'm using my Senior's code so forget about
var $this = $(this);


$("input[name=imagecheck]").each(function( index, element ) {
var $this = $(this);
if( $this.is(":checked") ){
console.log((index +1)+" : Checked");
alert($this.next("input").prop('name') +" -:- "+$(this).next("input").prop("type"));
alert($this.next("input").val() +" -:- "+$(this).next("input").val());
alert($this.nextAll("input").first().prop('name') +" -:- "+$(this).nextAll("input").first().prop("name"));
if($this.nextAll("input[type=file]").first().val() == '') {
//tempcounter = index +1;
$('#dialogSetter').text("Please Select the File of position ="+(index +1));
$( "#dialog" ).dialog( "open" );
//tempcounter = 0;
this.focus();
valid= false;
return false;
}
}
});//End of Checkbox Check Function


UPDATED

This is validation part for one of my
TAB
on clicking Next Button

Thank you

Answer

EDIT II :

Then first find your parent tr in which this is done and then find its td in which the file input is placed.

  $("input#imagecheck").each(function(index, element) {
    if( $(element).is(":checked") == true) {
      if($(this).closest('tr').find("td > input[type=file]").val() == '' ) {
        $('#dialogSetter').text("Please Select the File of position ="+(index +1));
          $( "#dialog" ).dialog( "open" );
          this.focus();
          valid= false;

          return false;
       }
     }
  });//End of Checkbox Check Function      

jsfiddle to play around : https://jsfiddle.net/8wpLyxbj/

Comments