Solved

.clone to a differet type

Posted on 2013-01-12
5
127 Views
Last Modified: 2013-04-22
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
Comment
Question by:tonelm54
  • 2
  • 2
5 Comments
 
LVL 25

Accepted Solution

by:
Kyle Hamilton earned 500 total points
ID: 38772156
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
 
LVL 8

Expert Comment

by:wal_toor
ID: 38778364
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
 
LVL 25

Expert Comment

by:Kyle Hamilton
ID: 38778412
walter,

I already posted that answer.
0
 
LVL 8

Expert Comment

by:wal_toor
ID: 38778429
@ kozaiwaniec

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

Author Comment

by:tonelm54
ID: 38826504
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
jquery not selector 1 13
(code works) but print excel in Landscape 5 49
C3-->D3 Line Chart 4 17
gif animation 6 17
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
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…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now