search

Home  >  Q&A  >  body text

My javascript function only works when the page is fully loaded

I want to disable the input text after the user clicks on another input tag with checkbox type,

The problem is that the date range input text is not disabled every time I click on the checkbox, can anyone help?

function myFunction() {
  document.getElementById("daterange").disabled = true;
}
<div class="item form-group">
  <label class="control-label col-md-3 col-sm-3 col-xs-12" for="email">Date <span class="required">*</span>
        </label>
  <div class="col-md-6 col-sm-6 col-xs-12">
    <input type="text" name="daterange" id="daterange" class="form-control date" placeholder="Choose multiple dates">
    <p style="color: white">
      <input type="checkbox" name="everyday" id="everyday" onclick="myFunction()" value="everyday" class="flat" /> Everyday
    </p>
  </div>
</div>

edit: I just realized that when the page is not fully loaded it works as I expected, but if the page is fully loaded there seems to be something that prevents the functionality from working

P粉320361201P粉320361201436 days ago469

reply all(1)I'll reply

  • P粉667649253

    P粉6676492532023-09-12 14:51:02

    Maybe you can try using alpineJS

    in laravel
    <div class="item form-group">
      <label class="control-label col-md-3 col-sm-3 col-xs-12" for="email">Date <span class="required">*</span>
            </label>
      <div x-data="{ inputDisabled: false }"  class="col-md-6 col-sm-6 col-xs-12">
        <input x-bind:disabled="inputDisabled"  type="text" name="daterange" id="daterange" class="form-control date" placeholder="Choose multiple dates">
        <p style="color: white">
          <input x-on:click="inputDisabled = true" type="checkbox" name="everyday" id="everyday"  value="everyday" class="flat" /> Everyday
        </p>
      </div>
    </div>

    If you use laravel brezze, alpine JS is already included. We can easily manipulate our DOM without writing a lot of JS code. Just write in inline HTML

    reply
    0
  • Cancelreply