SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Conveting jQuery functions to JavaScript

    Hi,

    I have the following two functions that is coded in jQuery and I want to convert them to pure JavaScript if possible.

    HTML Code:
    $(document).keyup(function(e) {
    	if(e.keyCode === 27) {
    		...
    	}
    });
    HTML Code:
    $(document).mouseup(function (e) {
    	var container = $('#box');
    	if (!container.is(e.target) && container.has(e.target).length === 0) {
    		...
    	}
    });
    Any tips will be appreciated, thanks.

  2. #2
    SitePoint Addict WolfShade's Avatar
    Join Date
    Mar 2014
    Location
    St. Louis, MO, USA
    Posts
    272
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Not tested, but it seems like:
    window.onkeyup = function(e){
    if(e.keyCode === 27){
    ...
    }
    }
    .. should work.

    Not sure about the second one.
    V/r,

    ^_^
    WolfShade The Spamhater

  3. #3
    SitePoint Addict
    Join Date
    Nov 2009
    Posts
    310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by WolfShade View Post
    Not tested, but it seems like:
    window.onkeyup = function(e){
    if(e.keyCode === 27){
    ...
    }
    }
    .. should work.

    Not sure about the second one.
    Yes, that worked. Thank you very much

    Tried this for the other one but didn't work:

    Code:
    window.onmouseup = function(e) {
    	var container = document.getElementById('box');
    	if (!container.is(e.target) && container.has(e.target).length === 0) {
    		...
    	}
    }

  4. #4
    SitePoint Addict WolfShade's Avatar
    Join Date
    Mar 2014
    Location
    St. Louis, MO, USA
    Posts
    272
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I'm pretty sure that ".is" isn't a JavaScript thing. I think it's jQuery. Perhaps try "if(!container === e.target)" for the first part. Again, not sure about the second part.
    V/r,

    ^_^
    WolfShade The Spamhater

  5. #5
    Gre aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    5,940
    Mentioned
    215 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    Give this site a go: http://youmightnotneedjquery.com/

    It offers vanilla alternatives to jQueryisms (if that's a word).

  6. #6
    SitePoint Addict WolfShade's Avatar
    Join Date
    Mar 2014
    Location
    St. Louis, MO, USA
    Posts
    272
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pullo View Post
    It offers vanilla alternatives to jQueryisms (if that's a word).
    If it isn't a word, "jQueryisms" gets my vote!
    V/r,

    ^_^
    WolfShade The Spamhater


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •