Automatically mark date checkbox

Asked

Viewed 92 times

0

I have 3 checkbox and 2 inputs. The second checkbox is parent() of 1 input type number and the third checkbox is parent() of 1 input type number:

$calendar .= "<td bgcolor='$color' data-semana=''><font size='2px'/> 
<input type='checkbox' name='arrachar[$year, $month, $day][dia]' value='$year-$month-$day' $marcado_data $disabled> <strong style='color:#5ca2df'>$year-$month-$day</strong> <br /> 
<div style='width:60%;position:relative;float:left'><input type='checkbox' name='arrachar[$year, $month, $day][OpcaoA]' value='Peq_Almoço' $marcado_pequeno $disabled> <strong style='color: #000000'>Peq. Almoço</strong></div> <div style='width:40%;position:relative;float:left'><input $disabled min='0' oninput='this.value = Math.abs(this.value)' type='number' name='arrachar[$year, $month, $day][Qtd]' value='$marcado_pequeno_qtd' style='width:65px; height: 22px' /> <br /> </div>
<div style='width:60%;position:relative;float:left'><input type='checkbox' name='arrachar[$year, $month, $day][opcaoB]' value='Almoço' $marcado_almoco $disabled> <strong style='color: #000000'>Almoço</strong></div> <div style='width:40%;position:relative;float:left'><input $disabled min='0' oninput='this.value = Math.abs(this.value)' type='number' name='arrachar[$year, $month, $day][Qtd1]' value='$marcado_almoco_qtd' style='width:65px; height: 22px' /> <br /> </div></font></center></td>";}

To automatically mark 2 and 3 checkbox by placing a value greater than zero in the input type number have this script:

<script> 
var inputs_ = document.querySelectorAll("[type='number'][name^='arrachar']"); 
for(var x=0; x<inputs_.length; x++){ 

inputs_[x].addEventListener("input", function(){ 

var box = this.parentNode.previousElementSibling.querySelector("[type='checkbox']"); 
box.checked = this.value > 0 ? true : false; 

}); 

}
</script>

When filling in 1 or 2 input type number, you also automatically mark the checkbox parent() of it, I also want you to automatically mark the first checkbox date. It is possible to adapt my script to do this?

The @osiris85 answer works in the example he gave, but when applying it in my code there is a problem, I will explain.

If I apply the @osiris85 response in my code, when filling the inputs, only selects the date of day 1 and not the date according to the day where the input is. For example: fill in inputs 2018-11-18 and automatically mark only checkbox of the day 2018-11-01

As I show in the picture: Exemplo do problema

1 answer

0

For this you can verify that always one of the values is positive if all are negative it would be discarded. To do this, you must assign an ID to the combo box and mark it or uncheck it, depending on the value of the rest.

var inputs_ = [...document.querySelectorAll("[type='number'][name^='arrachar']")]; 
for(var x=0; x<inputs_.length; x++){ 
  inputs_[x].addEventListener("input", function(){ 
    var box = this.parentNode.previousElementSibling.querySelector("[type='checkbox']"); 
    
    box.checked = !getValuesLessorEqualZero([this]);
    
    var firstBox = document.getElementById('firstCB');
    firstBox.checked =true;

    var valueAllLessOrZero = getValuesLessorEqualZero(inputs_);
    if(valueAllLessOrZero) firstBox.checked = false;
  }); 
}

const getValuesLessorEqualZero = (inputs) => {
  var lengthInputs = inputs.length;
  var valueLessOrZero = true;
  
  for(let i = 0; i < lengthInputs && valueLessOrZero; i++) {
    valueLessOrZero = inputs[i].value <= 0 ? true : false;
  }

  return valueLessOrZero;
};
<td bgcolor='$color' data-semana=''>
<font size='2px'/> 
<input id='firstCB' type='checkbox' name='arrachar[$year, $month, $day][dia]' value='$year-$month-$day' $marcado_data $disabled> 
<strong style='color:#5ca2df'>$year-$month-$day</strong> 
<br /> 
<div style='width:60%;position:relative;float:left'>
<input type='checkbox' name='arrachar[$year, $month, $day][OpcaoA]' value='Peq_Almoço' $marcado_pequeno $disabled> 
<strong style='color: #000000'>Peq. Almoço</strong>
</div> 
<div style='width:40%;position:relative;float:left'>
<input $disabled min='0' oninput='this.value = Math.abs(this.value)' type='number' name='arrachar[$year, $month, $day][Qtd]' value='$marcado_pequeno_qtd' style='width:65px; height: 22px' /> 
<br /> 
</div>
<div style='width:60%;position:relative;float:left'>
<input type='checkbox' name='arrachar[$year, $month, $day][opcaoB]' value='Almoço' $marcado_almoco $disabled> 
<strong style='color: #000000'>Almoço</strong>
</div>
<div style='width:40%;position:relative;float:left'>
<input $disabled min='0' oninput='this.value = Math.abs(this.value)' type='number' name='arrachar[$year, $month, $day][Qtd1]' value='$marcado_almoco_qtd' style='width:65px; height: 22px' /> 
<br /> 
</div>

  • I check that here is giving the result I want, but applying in my code the checkbox still does not appear marked

  • adcionó id to input <input id='firstCB' type='checkbox?

  • yes I added to html

  • try to remove the 3 tags </font></center></td>

  • the result is the same...

  • got an error in the developer console?

  • There is no error in the console and selects the date. But there is a problem, I put value on different inputs, just select the date of day 1 and not the date on which the input is. For example, I put value in the input of day 24 and select the date of day 1. I edited the question with the other part of the code.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.