Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically create and "click" a link with jQuery

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

like image 294
aidan Avatar asked Jun 28 '11 10:06

aidan


People also ask

How to create link dynamically in jQuery?

Answer: Use the jQuery . attr() Method You can use the jQuery . attr() method to dynamically set or change the value of href attribute of a link or anchor tag.

How to make a link clickable in jQuery?

Answer: Use the jQuery click() Method You can use the click() method to trigger a click on a link programmatically using jQuery.

How dynamically add HTML element using jQuery?

You can use jQuery DOM manipulation methods like append(), appendTo() or html() to add new HTML elements like div, paragraph, headings, image into DOM without reloading the page again.


1 Answers

Its not jquery, but it works just fine.

var link = document.createElement('a');
link.href = url;
document.body.appendChild(link);
link.click();    
like image 178
solipsicle Avatar answered Oct 13 '22 12:10

solipsicle