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

  • Home
  • SEARCH
  • 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 4554556
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:06:11+00:00 2026-05-21T17:06:11+00:00

I was looking for a way to create modules that could easily be included

  • 0

I was looking for a way to create modules that could easily be included in all kinds of projects. I didn’t want just the scripting in a separate file, but also template definitions and such. Since div’s can not be declared in a separate file I had to look for another way. What I can up with was this:

<head>
<title>DOMtest</title>

<script src='http://code.jquery.com/jquery-1.5.js'></script>

</head>

<body>
<script>

$(document).ready( function() {

    $("#holder").load( "template_link.html", function() {
        $("#test").append( clonelink( 1 ) );
        $("#test").append( clonelink( 2 ) );
        $("#test").append( clonelink( 3 ) );
    });

    $("#testbutton").click( function() {
        $("#test").append( clonelink( 4 ) );
    });


});
</script>

<div id='test' style='border: 1px solid black'>
<input type='button' id='testbutton' value='give me another clone'/>
</div>


<div id='holder' style='border: 1px solid black'>
</div>

</body>
</html>

then, in a separate file called template_link.html I had the following:

<script src='http://code.jquery.com/jquery-1.5.js'></script>

<div id='template_link'>
    This is a template<br />
    Let's see if this works or <span class='link_clicktest'>not</span>!
</div>

<script>

$("span.link_clicktest").click( function() {
    $d = $(this).closest( "div.link" );

    alert( "span " + $d.attr( "id" ) + " was clicked" );
});

function clonelink( $id ) {
    $c = $("#template_link").clone( true );
    $c.attr( "id", $id );
    $c.addClass( 'link' );

    alert( $id + "-" + $c.html() );
    return $c;
}

</script>

At first it seemed to work. I have declared a span in the template. If you click on “not!” you’ll get the ID of that span. Cloning the first 3 seems to work, they show up on screen the way they’re supposed to. But then…

I clicked the button on there “give me another clone”. The clone is indeed created. With the alert in the clonelink-function I can see the ID gets there. But even with the ‘true’ option enabled in the the .clone()-call it doesn’t seem to clone the functionality of the span.

Am I overseeing something or am I trying to do something that can not be done in this way?

  • 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-05-21T17:06:11+00:00Added an answer on May 21, 2026 at 5:06 pm

    I figured out a workaround…

    Instead of putting the functionality in the template I defined the functions for each clone, so the function clonelink changed to:

    function clonelink( $id ) {
        $c = $("#template_link").clone();
        $c.attr( "id", $id );
        $c.addClass( 'link' );
    
        $c.find(span.link_clicktest").click( function() { 
            $d = $(this).closest( "div.link" );
            alert( "span " + $d.attr( "id" ) + " was clicked" );
        });
    
        alert( $id + "-" + $c.html() );
        return $c;
    }
    

    Now for every clone that is created the click function is bound again. The .clone(true) is replaced with a .clone() ’cause the functions bound to it are redefined anyway. This works like a charm, but I’m still puzzled why the first example in the original post didn’t work. There is a working DIV on the screen which is cloned with .clone(true) and still the .click-function isn’t bound to the defined span…

    If anyone has a more elegant solution I’d be delighted to hear about it. Also, I’d like to know why the first example isn’t working correctly…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.