Hello everyone, people are starting to learn js, here are some scripts:

$(document).ready(() => { $("#rowPerPage").on("change", () => { var pages = $("#rowPerPage>option:selected").text(); //var pages = 15; var form = $("form.search.pull-left"); var input = $("<input>").attr( {"type": "", "name": "pages", "id": "pages", "value": pages } ); $(input).appendTo(form); document.getElementById("#filtAccept").click(); }); }); 

here is the form:

  <form class="search pull-left" style="padding-right: 5px;" method="get" name="frm" action="Search"> <label>Що</label> <input type="text" minlength="3" size="12px" name="osdch" value="${osdch}" id="osdch" align="middle" placeholder="Осд(Що)"> <label>Куди</label> <input type="text" minlength="3" size="12px" name="osdk" value="${osdk}" id="osdk" align="middle" placeholder="Осд(Куди)"> <label>Код виробу</label> <input type="text" size="12px" name="kiz" value="${kiz}" id="kiz" align="middle" placeholder="Код виробу"> <input type="hidden" name="page" id="page" value="1"> <input type="hidden" name="count" id="count" value="0"> <% long curTime2 = System.currentTimeMillis(); String svi = new SimpleDateFormat("dd.MM.yyyy").format(curTime2); %> <input type="hidden" name="svi" id="svi" value="<%=svi%>"> <button id="filtAccept" class="btn btn-md btn-success" data-toggle="tooltip" data-placement="bottom" title="Виконати фільтр"> <span class="glyphicon glyphicon-ok"></span></button> </form> 

And the actual question is, why doesn’t the button automatically work ?: document.getElementById("#filtAccept").click();

  • one
    Because you have an error in the script. Look for errors in the console. - Stepan Kasyanenko
  • five
    The getElementById ID method must be passed without a # . - Regent

1 answer 1

Because click is a jQuery method, and you are trying to use it to work with a native html element obtained in the normal way. You need either

 $("#filtAccept").click(); 

Or

 document.getElementById("filtAccept").dispatchEvent(new Event('click')); 

UP: Bes confused, so naturally you can:

 document.getElementById("filtAccept").click(); 
 <button id='filtAccept' onclick='console.log(1)'>Button</button> 

  • Not true, the click method is not only in jQuery: developer.mozilla.org/ru/docs/Web/API/HTMLElement/click - Regent
  • did so, so can i? document.getElementById("filtAccept").click(); - Sorpok
  • @ SergeyNikonenko Updated the answer - Darth
  • You can also document.querySelector("#filtAccept").click() . - Yaant
  • Thank you very much for your help - Sorpok