Solved

.clone to a differet type

Posted on 2013-01-12
5
146 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
[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
  • 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

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

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

Suggested Solutions

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

734 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