Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9159631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:32:24+00:00 2026-06-17T13:32:24+00:00

Possible Duplicate: jQuery keypress() event not firing? I am using wordpress as my cms

  • 0

Possible Duplicate:
jQuery keypress() event not firing?

I am using wordpress as my cms

and am also devloping a voting system

This is my jquery for voting function

How can i run this function on keypress event ?

For example if someone press Key “A” this function automatically runs

I did google for atleast half an hour but didnt find any relevant results

 <script type="text/javascript">
/* <![CDATA[ */ 
(function($) {
 function setCookie(name,value,days) {
 if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
 }

 $(document).ready(function(){
 $("body").on('click', '.vote-btn:not(.disabled)', function () { 
 var el = $(this),
    nonce = $("input#voting_nonce", el.parent()).val(),
    id = el.attr('id').replace(/vote-/, ''); // get the Post ID

 el.html('<span id="loader"></span>');
 var data = {
    action: 'add_votes_options',
    nonce: nonce,
    postid: id,
    ip: '<?php echo $_SERVER['REMOTE_ADDR']; ?>'            
};
 $.post('<?php echo admin_url('admin-ajax.php'); ?>', data,
function(response){
 console.log( response );

     if(response!="-1") {

el.html('<img src="<?php get_bloginfo('url'); ?>/wp- content/themes/9GAG/happysimily.png">').unbind('click').addClass('clickedyellow');
 $('.vote-btn', el.closest('div')).addClass('disabled');
 $("#vote", el.closest('article')).addClass('clickedlove'); 



        if(response=="null") {
            alert("A vote has already been registered to this IP address.");

        } else {
            $("#votecounter", el.closest('article')).html(response); 


        }
        var cookie = getCookie("better_votes");
        if(!cookie) {
            var newcookie = id;
        } else {
            var newcookie = cookie + "," + id;
        }
        setCookie("better_votes", newcookie, 0);
    } else {
        alert("There was a problem registering your vote. Please try again later or enable browser cookies.");
    }

 });

 return false;
});

 })
 })(jQuery);




 /* ]]> */
 </script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T13:32:25+00:00Added an answer on June 17, 2026 at 1:32 pm

    You can capture keypress in different ways , simple one is use Jquery default one,

    .keypress()

    So you need to assin ur function block to a function name. and this should be inside your document ready function since your voting function is not global and in context of ready only.

    (function($) { 
    
       var yourVotingFn = function(){
              .........//your code logic
        }
    
       $(body).keypress(function(event) {
         if ( event.which == 13 ) {//Enter key
           yourVotingFn();
         }
       });
    
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: jQuery voting system I've been using this tutorial to create a voting
Possible Duplicate: jQuery Event Keypress: Which key was pressed? I'm using below code to
Possible Duplicate: jQuery: more than one handler for same event I am using a
Possible Duplicate: JQuery .each() backwards I am using this code to show each div
Possible Duplicate: jQuery $(this) vs this When using jQuery, whats the difference between this
Possible Duplicate: jQuery Event Keypress: Which key was pressed? OK, here's my situation (I've
Possible Duplicate: jQuery AutoComplete Trigger Change Event I am using jQuery UI v1.9 and
Possible Duplicate: Find text string using jQuery? How do you find a text string
Possible Duplicate: jQuery get input value after keypress I'm trying to get an input
Possible Duplicate: Jquery validation - allow number without the leading zero? I am using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.