Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

copy handler

Posted on 2013-01-12
1
Medium Priority
?
164 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
[X]
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
1 Comment
 
LVL 1

Accepted Solution

by:
headshock earned 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…
Suggested Courses

661 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