Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

.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
0
tonelm54
Asked:
tonelm54
  • 2
  • 2
1 Solution
 
Kyle HamiltonData ScientistCommented:
You could trigger the click event of the li when the div is clicked. This will execute whatever handler is associated with the li1 click event when div1 is clicked.

$('#div1').on('click', function(){

      $('#li1').trigger('click');
});

Open in new window

0
 
wal_toorCommented:
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
0
 
Kyle HamiltonData ScientistCommented:
walter,

I already posted that answer.
0
 
wal_toorCommented:
@ kozaiwaniec

You are right, sorry about that, just trying to help. p.s. made a new jsfiddle: http://jsfiddle.net/rUHzT/4/
0
 
tonelm54Author Commented:
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
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now