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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:28:26+00:00 2026-06-02T04:28:26+00:00

Hello i am written some script in jquery and bind that to a click

  • 0

Hello i am written some script in jquery and bind that to a click event, but the code do not get activated by click event. But when i put the script in another JavaScript function and bind that JavaScript function to click event listener than the function is able to execute on clicking.

Another thing is that when i bind the script using JavaScript it returns me alert message same number of time as the number of div on which i have clicked i.e. if i click on 3rd div the alert message is shown three times
on my page the dom structure is of pdf.js project and this dom is created dynamically
i want to get the index of grandchild of pagecontainer2 on which user had clicked
following is my script:

<script type="text/javascript">
    $(function() {
        $(".textLayer > div", "#pageContainer2").click(function () {
            var index = $(this).index();
            alert("index of div is = " + index);
        });
    });
</script>

Dom CODE :

<div id="pageContainer2" >
    <canvas id="page2" width="741" height="959"></canvas>
    <div class="textLayer">
        <div>......some text here....</div>
        <div>......some text here....</div>
        <div>......some text here....</div>
    </div>
</div>

<div id="pageContainer3" >
    <canvas id="page3" width="741" height="959"></canvas>
    <div class="textLayer">
        <div>......some text here....</div>
        <div>......some text here....</div>
        <div>......some text here....</div>
    </div>
</div>​
  • 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-02T04:28:27+00:00Added an answer on June 2, 2026 at 4:28 am

    If you need to assign a click event handler to a DOM node that is not yet present but will be created later, you need to bind the event handler to an existing node using on and specify the child nodes:

    Let’s say that <div id="pageContainer2"> already exists, but you’re creating those divs inside that container on the fly. So the proper sytnax would be:

    $(function() {
        $('#pageContainer2').on('click', '.textlayer > div', function() {
            /* .... */
        });
    });
    

    That way, you’re actually binding the event handler to the container, but since most events bubble up the DOM, an event fired by a click on any element inside that container will reach the container itself. Because you specified another selector (`.textlayer > div´), jQuery will then check if the actual element that got clicked on satisfies this selector.

    If even #pageContainer2 is created after binding the event handler, you can still bind the event handler to the document itself:

    $(document).on('click', '#pageContainer2 .textlayer > div', function() { ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello there i have written some code in jquery to add new input type
I have written some jquery to get around this problem: iPhone "Bookmark to Homescreen"
I'm having a really nasty problem with some code that I've written. I found
Hello, I’ve written a script for autocomplete like Facebook; it works well on a
I've written a simple 'hello world' app which runs fine in an emulator, but
Hello! I'm not even sure if this is possible, but hopefully it is in
I'm learning javascript and jquery and have written a very basic script inside my
i have written a library with some classes that make use of qt object
Hello i've been working to get som json to work. I've written this little
I am writing some code that traverses a structure that may have cyclic references.

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.