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 7963137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:32:35+00:00 2026-06-04T05:32:35+00:00

I am doing like this : $(.classname).bind(keypress,function(){ alert(event happened); }) code similar to above,

  • 0

I am doing like this :

$(".classname").bind("keypress",function(){
    alert("event happened");
})

code similar to above, working only once, I mean, the first time you type and click enter, it’s working, but next time, its not at all reacting.

$("#id").bind("keypress",function(){
   alert("haiii");
}) 

the second code working all the time, but the first code working only once.

Also if second code is run once, the first code is not even running once.

What is the solution? I think I am missing some rules here, can you tell them so that I will search about them.
Thanks

  • 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-04T05:32:37+00:00Added an answer on June 4, 2026 at 5:32 am

    The event binder should be always available; if it’s not it’s because you’re changing the HTML structure (either appending or deleting nodes). In your case, you’re dynamically changing HTML at runtime, you need to use .on()

    Try this instead of .bind():

        $('#id').on({
           keypress: function () { alert("hi"); }
        });
    
        $('.ClassName').on({
           keypress: function () { alert("hi"); }
        });
    
        // IF YOU KNOW CLASSNAME ELEMENTS ARE INSIDE A FIXED ELEMENT:
    
        $('#FixedID').on({
           keypress: function () { alert("hi"); }
        }, '.ClassName');
    

    Regarding your coding style, you should separate the event handlers and the functions that handle the events. For instance, instead of this where the handlers also execute code:

    // one function that does everything!!
    $(document).ready(function() {
        // bind events
        $('#SomeID').on({
    
           click: function () {
             // huge wall of code that handles events
           },
           mouseenter: function () {
             // another huuuuuuuge wall of code here
           }
        )};
    });
    

    You should have something like this:

    $(document).ready(function () {
        BindHandlers();
        DoSomethingElseWithPageInit();
    }); 
    
    function BindHandlers() {
    // handlers do just the handling of events; easy to see and understand
       $('#SomeID').on({
          click: function (e) { ClickOnSomeID(e); },
          mouseenter: function () { MouseEnterOnSomeID(); }
       )};
    }
    
    // here are the functions that process the event
    function ClickOnSomeID(e) { ... }
    function MouseEnterOnSomeID() { ... }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently doing something like this: myFunc(tracer); function tracer(message:String):void{ trace(message); } function myFunc(callback:Function):void{
when doing like this: const int a = 5; I wonder if a will
i am using ASIHTTPRequest in my iOS APP. i am doing like this :
When parsing an xml file in android, I'm doing like this: try { InputStream
I am doing something like this in myproject.myapp.urls : from django.conf.urls.defaults import * urlpatterns
I'm currently doing it like this: MessageQueue queue = new MessageQueue(.\Private$\myqueue); MessageEnumerator messageEnumerator =
I am doing something like this- http://www.w3schools.com/html5/tryit.asp?filename=tryhtml5_video_js_prop but I want to replace the buttons
I am doing something like this in SP, insert into @FilteredTbl select * from
I am talking about doing something like this: LOCK TABLE page WRITE; SELECT *
I am developing a program where I find myself doing this like this a

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.