Triggering a click event on element creation
Hello. I have a function where I want to add a sibling element to each of the elements and trigger a click on each of them right away. Here's what I have so far:
- $(function(){
- $('#articles').delegate('.click-link','click',function(){
- $(this).toggle(
- function(){
- alert('toggle one');
- },
- function(){
- alert('toggle two');
- }
- );
- });
- $('.toggle').each(function(){
- $(this).after('<span class="click-link">Hide</span>');
- $(this).next().trigger('click');
- });
- });
For some reason the toggle doesn't get triggered. If I put something in the function right before the toggle, it fires on page ready. However, the toggle doesn't fire. toggleClass does, though. What can be causing this?
Thanks!
Luka