Kirill Nagiev Kirill Nagiev - 1 month ago 7
HTML Question

Check if text input is not empty

I do have a checkbox and input field which becomes enabled after checking chekbox. What I want is to make input field already enabled when user wants to edit record. Means that after user checks checkbox and fills the input and after submitting if he wants to modify it field should be already enabled.

What i have in javascript:

$(document).ready(function () {
$('.checkbox').change(function() {
$(this).parent().next().find('.field').prop('disabled', !$(this).is(':checked'))
});
});


And expanded checkboxes:

<div class="row">
<div class="small-8 large-8 columns">
<label>Process: <small style="color:red;">*</small>
<div class="multiselect">
<div class="selectBox" onclick="showCheckboxes()">
<select onclick="showCheckboxes()">
<option>-- Select an option --</option>
</select>
<div class="overSelect"></div>
</div>
<div class="scrollable" id="checkboxes">
<?php
while ($row = mysqli_fetch_array($result))
{
$row[0] = cleanOutputData($row[0]);

if(isset($process))
{
foreach($process as $code)
{
if($row[0] == $code)
{
$match = 1;
break;
}
else
$match = 0;
}
}
if(isset($requiredNo) && isset($process))
{
foreach($process as $key => $code)
{
if($row[0] == $code && $requiredNo[$key] != 0)
{
$match4 = 0;
break;
}
else
$match4 = 1;
}
}
?>
<div class="row">
<div class="small-12 large-12 columns">
<label style="height: 37px; width:80%; float:left;" >
<input type='checkbox' class="checkbox" style="margin-left:5%; width:15%;" name='process[]' id=<?php echo $row[0] ?> value="<?php echo $row[0] ?>" <?php if (isset($process) && $match==1) echo 'checked="checked"' ?>/><?php echo $row[0] ?>
</label>

<label style="width:40%; margin-left:60%;">
<input type="text" class="field" disabled style="width:40%;" name="numberpl" id="<?php echo $row[0] ?>" value= "<?php if (isset($requiredNo) && $match4==0) echo $requiredNo[$key] ?>" required/>
</label>
</div>
</div>

<?php
}
mysqli_free_result($result);
?>
</div>
</div>
</label>
</div>
</div>


So question is how I can make a field enabled if it is not empty? Thanks

Answer

You could try to trigger checkbox at finished loading:

$(document).ready(function () {
        $('.checkbox').change(function() {
          $(this).parent().next().find('.field').prop('disabled',!$(this).is(':checked'));
        });
$('.checkbox').trigger('change');
});
Comments