I have a <span class="clickspan">Some text here</span>
What I'm trying to do is when the span
is clicked a dynamic anchor is created for the clicked span and then click
event is triggered with jquery on the dynamic anchor.
$('.clickspan').on('click', function(){
// Method 1: Replace span with dynamic anchor
$(this).replaceWith('<a id="1234" href="www.example.com"></a>');
// None of the following works
$('#1234').trigger('click');
$('#1234').click();
// Method 2: Insert dynamic anchor inside the span
var theSpan = $(this).wrapInner('<a href="'+data.msg+'" donwload></a>'),
anch = theSpan.find('a');
// this click event on the dynamic anchor inside the span causes the click
// event on the parent span and to run the entire script all over again.
anch.click();
});
I've been struggling with this for a while and exhausted all ideas, so I'd appreciate any suggestion.
SOLVED
The method and the source suggested by @Ninsly in the comment below fixed the issue. The following worked:
$('#1234')[0].click();
Sorry for taking everybody's time. I was not aware of the need to use [0]
.