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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:56:36+00:00 2026-06-04T13:56:36+00:00

click event handler on add button is not working if i do following(click event

  • 0

click event handler on add button is not working if i do following(click event defined in add.js and ready event is present in index.js).

If i put click event handler and ready handler in index.js, click event works (alert popup).

Please help me.

Note: #add_button is a static button ( present in index.php, not a dynamic div, i tried live instead of click but that doesn’t work too).

This doesn’t work

<script src="js/index.js" type="text/javascript"></script>
<script src="js/add.js" type="text/javascript"></script>

index.js

$(document).ready(function() {
//code
});

add.js

$('#add_button').click(function(event){
 alert('hello');
});

this works if add.js content is directly put into index.js ?

$(document).ready(function() {
//code
$('#add_button').click(function(event){
 alert('hello');
});
});
  • 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-04T13:56:37+00:00Added an answer on June 4, 2026 at 1:56 pm

    It’s not because they’re in different files. It’s because the code hooking up the click event is running too soon. By putting the click setup inside ready, you’re making it wait until “DOM ready”. When it’s not in the ready handler, it runs right away. If that code is above the #add_button element, the element won’t exist yet, and the handler won’t be hooked up.

    You have two options:

    1. Just put the script tag for add.js below the element(s) it uses in the markup, e.g.:

      <input type="button" id="add_button" value="Add">
      <!-- ... -->
      <script src="add.js"></script>
      

      Then when the browser runs add.js, the element will exist. The script tag could even be immediately after the button, but I’d usually recommend the end of the file (e.g., just before the closing </body> tag), as indeed do the YUI folks.

    2. Use another ready handler in add.js (you can have as many as you like):

      // add.js gets its *own* ready handler
      $(document).ready(function() {
          $('#add_button').click(function(event){
           alert('hello');
          });
      });
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an image with a click event handler that captures the location where
I have a Raphael text element with both a mouseup and click event handler.
my UI is <li><a href='#'>hello</a></li> anchor tag has already event handler i.e. click(function(){}) I
I have a click handler, which has event.preventDefault. There is whole lot of logic
i careate button control and place in panel but not working? protected void Button1_Click(object
My application uses a TButtonGroup control. I assign to each button an event handler:
I have this method defined since I will add a link button to each
asp:Button inside SharePoint Dataview Rows with onclick event does not call C# function. Pull
Problem: Creating an Element on a button click then attaching a click event to
I am following this thread to disable/ enable the click on a button, Best

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.