Dynamically create and "click" a link with jQuery

aidan picture aidan · Jun 28, 2011 · Viewed 56.3k times · Source

I want to dynamically create an <a href="mailto:..."> element, then "click" it. All without modifying the page.

I'm trying this:

$('<a href="mailto:[email protected]">&nbsp;</a>').click();

...to no avail

Answer

solipsicle picture solipsicle · Mar 1, 2013

Its not jquery, but it works just fine.

var link = document.createElement('a');
link.href = url;
document.body.appendChild(link);
link.click();