Link to home
Start Free TrialLog in
Avatar of tonelm54
tonelm54

asked on

.clone to a differet type

Im trying to copy the onclick event from a list item, to a div.

Eg:-
<ul>
     <li onclick="alert('hi1');" id="li1">1</li>
     <li onclick="alert('hi12);" id="li2">2</li>
     <li onclick="alert('hi3');" id="li3">3</li>
</ul>

Open in new window


What Id like to do is copy the onclick events to a generated div:-
<div>
     <div id="div1">1</div>
     <div id="div2">2</div>
     <div id="div3">3</div>
</div>

Open in new window


I have tried:-
      var myFunction = $('#li1').Click;
      $('#div1').click(myFunction);

Open in new window


As well as:-
     $('#div1').Click =  $('#li1').click();

Open in new window


I have also tried using the clone method, which hopefully would copy all functions:-
     $('#div1') = $('#li1').clone( true )

Open in new window

However from what I've read the clone cannot be used to copy handlers between events (eg list items and divs).

Anyone have any other suggestions on copying handlers between different elements?

Thank you
ASKER CERTIFIED SOLUTION
Avatar of Kyle Hamilton
Kyle Hamilton
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

Made a little jsfiddle of your list example (assuming you are using jquery).

http://jsfiddle.net/rUHzT/

Hope this works for you.

greetz,
walter
walter,

I already posted that answer.
@ kozaiwaniec

You are right, sorry about that, just trying to help. p.s. made a new jsfiddle: http://jsfiddle.net/rUHzT/4/
Avatar of tonelm54
tonelm54

ASKER

Ok, Im not sure Im reading off the same page here.

Im trying to copy an even from a div onclick event to a list onclick funtion .

I cant see the jsfiddle at the moment, as I just get a bad gateway error at the moment