Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: key exception

  1. #1
    Regular Coder
    Join Date
    Nov 2011
    Posts
    154
    Thanks
    5
    Thanked 0 Times in 0 Posts

    key exception

    I would like to have Spacebar work normally only in inputs and textareas. I thought it'd be something like this but no luck..

    $(document).keydown(function(e) {
    if (e.keyCode == 32) return false;
    });

    $('input, textarea').keydown(function(e) {
    if (e.keyCode == 32) return;
    });

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,722
    Thanks
    25
    Thanked 662 Times in 661 Posts
    You don't show what is calling this function. That needs to be done in order to use the return.
    If you don't have a calling function you should just do the assign in the code you gave, something like this:
    Code:
    <body>
    <div id="myDiv">
    <form>
    	<textarea  id="tarea" style="width: 400px; height: 100px">some text</textarea><br />
    	<br />
    </form>
    
    	<span>hello there</span>
    </div>
    
    <input type="text" size="13"/>
    <div id="disp">A.)</div>
    
    <script type='text/javascript' src='javascript/jquery.js'></script>
    <script>
    $(document).keydown(function(e) {//alert('here');die;
    	if (e.keyCode == 32) $("span").after("<i>After</i>");
    });
    
    $('input, textarea').keydown(function(e) {
    	if (e.keyCode == 32) keyCode = 32;
    });
    </script>
    </body>
    </html>
    Try a space after the A.)
    Last edited by sunfighter; 03-12-2014 at 04:03 PM.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    I think you should just return false on document keydown and return true on input/textarea keydown if keyCode is 32.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    i think you mean this:

    Code:
    $(document).keydown(function(e) {
       if (e.keyCode == 32 && !e.target.type) e.preventDefault();
    });
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    Quote Originally Posted by resin View Post
    Sorry I'm not familiar with target type. Are you saying the complete code should be this?

    $(document).keydown(function(e) {
    if (e.keyCode == 32 && !e.target.type) e.preventDefault();
    });

    $('input, textarea').keydown(function(e) {
    if (e.keyCode == 32) return true;
    })
    you only need the first one i posted...
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%


  •  

    Posting Permissions

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