1

I have a function to display my wordpress search form, and to hide it. The problem is that I want all the container to be clickable to display/hide, but when I click the text input of my search form it hide it. Is it possible to not trigger the onclick event when I click on the input ?

Here is my architecture :

<li onclick="search_form(this)">
  <img src="#" >
  <div>
      <form role="search" id="searchform" method="get" action="/">
          <div>
              <input type="text" name="s" id="s" size="15" required="" placeholder="Search">
          </div>
      </form>
  </div>
</li>

I have something like that https://puu.sh/t3tiN/8655c07973.png (picture of my li)

Thank you

Edit : Someone asked me for the function, didn't want to add it because it's just css :

function search_form() {
  if ($(window).width() > 1041) {
      var display = $('#mobile_search_form').css('display');
            if (display == "none") {
                  /* many css animate */

            } else if (display == "inline-block") {
                  /* many css animate */
            }
    }else if ($(window).width() < 1041) {
      window.location.href = "#"
      $(document).ready(function () {
        $('#result_number').css('display',"none");
      });
    }
}

2 Answers2

0

Take a look at stopPropagation(). I think this is exactely what you want to do:

<li id="myLi">
  <img src="#" >
  <div>
      <form role="search" id="searchform" method="get" action="/">
          <div>
              <input type="text" name="s" id="s" size="15" required="" placeholder="Search">
          </div>
      </form>
  </div>
</li>

Javascript:

$("#myLi").click(search_form($(this));
$("#s").click(function (e) {
    e.stopPropagation();
});
elementzero23
  • 1,389
  • 2
  • 16
  • 38
0

If you don't mind a bit of inline script:

<li onclick="search_form(this)">
  <img src="#" >
  <div>
      <form role="search" id="searchform" method="get" action="/">
          <div>
              <input onclick="event.stopPropagation()" type="text" name="s" id="s" size="15" required="" placeholder="Search">
          </div>
      </form>
  </div>
</li>

Credit: I got the answer from How to stop event propagation with inline onclick attribute?

Community
  • 1
  • 1
vothaison
  • 1,646
  • 15
  • 15