javascript - Join events keypress (with keyCode) and click on link -


My question is simple but I could not find any solution.

I have a function:

  function toggle label () {$ (".ableable"). Toggle (); If ($ (this) .Parent () .Parent (). HasClass ("selected")) {$ (this). Parent (). Parent (). RemoveClass ("selected"); $ ('Preview-toggle message'). Text ('Hide Options'); } And {$ (this). Parent (). mother-father (). AddClass ("Selected"); $ ('Preview-toggle message'). Text ('show options'); }}  

that I would like to call with two different incidents:

  $ (document) .keypress (function (e) {if (e Click the keycode (= 113) {....  

and

  $ ('preview-toggle one').   

Is it possible to get involved in these events? I have tried .bind () but without any positive result: (

thanks.fabio

After trying my suggestions, I decided to separate the events and choose this format. Is:

  var toggleLabels = function {$ (".ableable") toggle (); if ($ ($ '(preview' toggle '). $ ('. '$' ('Preview-toggle message') .text ('hidden options');} and {toggle margin ("hide"); $ ('preview-toggle'). AddClass ("selected") ; $ ('Preview-toggle message'). Text ('show option');}} $ ('preview-toggle a'). Click (function () {toggle label ()}); $ ( Document) .keydown (function (e) {if (e.keyCode == 113) {toggleLabels ()}});  

B Both events have been triggered on different elements ( '.preview-toggle a' document) and different types of them that you can not contain. Since the click-event bubbles the DOM and accesses the document, you can set the handler for the document like this:

  var links = $ ('preview-toggle one' ); $ (Document) .on ('keypress click', function (event) {if (event.keyCode == 113} event.type == 'click' & amp; amp; links.index (event.target) & gt; - 1) Toggle label ();});  

But note that the jQuery under the hood sets two listeners on the document, for a 'keypress' and a 'click'. The only other way is to use a function similar to callback in each handler:

  $ ('preview-toggle one'). Click (toggle label); $ (Document) .keypress (function (e) {if (e.keyCode == 113) toggleLabels ();})  

Also keep in mind that both the way - The code inside the toggle label indicates the various elements You may have to $ (this) to $ ("some selector") :

: You should not create a jQuery-object. The same content many times if you store it once and in the context of that variable it is required :

  function toggle label () {$ ("hideable"). Toggle (); Var par = $ (this). Parent (). Parent (); // Store jQuery object here (par.hasClass ("selected")) {// reference variable par.removeClass ("selected"); // reference again $ ('preview-toggle message'). Text ('Hide Options'); } Other {par.addClass ("selected"); $ ('Preview-toggle message'). Text ('show options'); }  

}


Comments

Popular posts from this blog

HTML/CSS - Automatically set height width from background image? -

php - Mysql Show Process - Sleep Commands and what to do -

c - What is the address of buf (the local variable in the main function)? -