May 15, 2012

jQuery function enable a div

Question by user1213807

I’m this code embed my js file for win key close. This codes:

jQuery(document).keydown(function(e){
    alert(e.keyCode); // replace "37" below with the windows key code
    if (e.keyCode == 37) { 
       alert( "windows key pressed" );
       return false;
    }
});

Working good. But i want only work 1 div. How can i do this?

Answer by mprabhat

Add the selector instead of document

jQuery('selector') instead of jQuery(document)

where selector can be anything like id, class e.t.c

Demo which uses selector as id

Another Demo will work for only one input inside a div.

Div Demo will work only for first div

Disabled for Div will work only for span not for div

Disabled for input in div will work for all input if they are not inside a div

Answer by Starx

Use your div selector instead of document selector.

jQuery("#yourdivid").keydown(function(e){
    alert(e.keyCode); // replace "37" below with the windows key code
    if (e.keyCode == 37) { 
       alert( "windows key pressed inside div" );
       return false;
    }
});

To disable a div, you can simple use $("#yourdivid").prop('disabled', true);

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!