Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The jquery "on click" function gets triggered on key press "enter"

Tags:

jquery

jQuery(document).ready(function() {
    jQuery(".multipleDataRow").on("click", "[id^='multiremove_']", function() { 
        //code here to delete
    });
});

I want to avoid remove function from being called on key press "enter"

this on click function is triggering when i focus on other textbox and key press enter button.

the remove button is inside the container having class "multipleDataRow".

The remove function keeps on triggering on key press enter on other fields.

Edit: HTML from coment

<div class="multipleDataRow">
  <label class="">Skills</label>
  <div class="">
    <div id="" class="">
      <div id="multiRow_2_Skillset" class="mBtm-5 span8 mLft-0">
        <input type="text" placeholder=".." id="skill" name="skills[]" value="" class="m-wrap" readonly="readonly">
        <select name="data[tmp_experiences[]]" id="tmp_years_exp_2" class="m-wrap" disabled="disabled">
          <option value="1">1</option>
        </select> Yrs&nbsp;
      </div>
      <div id="spanMultiremove_Skillset_2" class="mBtm-5">
        <button id="multiremove_Skillset_2" skillset_skexp="59" class="btn mini red"> remove </button>
      </div>
    </div>
  </div> <!-- Missing in comment -->
</div> <!-- Missing in comment -->
like image 757
George Joffin Joy Avatar asked Oct 09 '13 13:10

George Joffin Joy


People also ask

How do you trigger click event on pressing Enter key?

To trigger a click button on ENTER key, We can use any of the keyup(), keydown() and keypress() events of jQuery.

What is trigger click in jQuery?

jQuery trigger() Method The trigger() method triggers the specified event and the default behavior of an event (like form submission) for the selected elements. This method is similar to the triggerHandler() method, except that triggerHandler() does not trigger the default behavior of the event.

Which jQuery event occurs when key is pressed?

The keypress() method triggers the keypress event, or attaches a function to run when a keypress event occurs. The keypress event is similar to the keydown event. The event occurs when a button is pressed down.

When Enter is pressed jQuery?

The “enter” key is represent by code “13”, check this ASCII charts. To check if an “enter” key is pressed inside a textbox, just bind the keypress() to the textbox. $('#textbox').


1 Answers

Pressing the Enter Key whilst focused on a button is treated in the same way as a click event by the browser.

As Rory has mentioned, this is a feature by design of all Browsers, if you wish to negate this feature end users may be unhappy with the end result as it will not have all the Standard functionality.

To negate this you could have a check for onkeydown/onkeyup/onkeypress to check whether a keyboard action occurs prior to the click event firing and stop the click events code firing if a key was pressed.

i.e.

var isKeyPress = false;

document.onkeydown = function(event) {
   if (event.keyCode == 13) {
      isKeyPress = true;
   }
}

jQuery(".multipleDataRow").on("click", "[id^='multiremove_']", function() { 
   if (!isKeyPress) {
      //code here to delete
   }
   isKeyPress = false;
});

This should stop the click events code running if the isKeyPress value is true

like image 136
Nunners Avatar answered Oct 13 '22 12:10

Nunners