Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

copy handler

Posted on 2013-01-12
1
157 Views
Last Modified: 2013-02-14
I'm wondering if there is a way using jQuery to copy a handler onto another component. What Im trying to do is convert one element to another, but if there is a handler, Im want to copy it to the new element.

So for example:-
$(document).ready(function() {
      var elm = $('#btnTest');
      var value = $(elm).val();
      var myFunction = $(elm).Click;
      var thisUID = uid();
      $('#wrapper').html('<div id="' + thisUID + '" class="childDiv">' + value + '</div>');
      $('#'+thisUID).click(myFunction);
    });

function uid() {
   var result='';
   for(var i=0; i<32; i++) {
       result += Math.floor(Math.random()*16).toString(16).toUpperCase();
       }
   return result
  }

Open in new window


Ive been reading a few articles about cloning handlers (hence having to put an ID in), but I cant get one working.

Ive put it on jsFiddle to show what Im trying to do http://jsfiddle.net/5yUwS/ in the hope someone can assist
0
Comment
Question by:tonelm54
1 Comment
 
LVL 1

Accepted Solution

by:
headshock earned 500 total points
ID: 38839438
Are you trying to copy the ID handler and apply it to another element? If so I suggest any operations on the handler are done using .on and the previous instance is destroyed or renamed to avoid duplicate IDs. obviously use a class to to the event trigger.
$(".classname").on("click",function(event){
 // events
});

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question