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

.clone to a differet type

Posted on 2013-01-12
5
142 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Requirements JQuery 1.6+ HTML CSS Introduction This article was inspired by an EE question (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28372511.html) on how to make a page show some balloons animate up a page…
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…

838 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