Konrad Viltersten
Konrad Viltersten

Reputation: 39058

How to create a link in jQuery in a valid way?

At this site, the blogger suggest a creation of a link using the syntax below.

$('<a>clicky clicky</a>', { href: dataLink, download: fileName });

However, I don't get it to work. I need to use the syntax as follows.

$('<a href="' + dataLink + '">clicky clicky</a>');

Not being familiar with the former, I wonder if I'm missing something. Is that a valid jQuery?

Also, tha tbegs the question of what to do with the download attribute. Is the first line of code equivalent to the one below? I have a feeling it's not.

$('<a href="' + dataLink + '" download="' + dataLink + '">clicky clicky</a>');

Upvotes: 1

Views: 71

Answers (2)

user2575725
user2575725

Reputation:

Use prop():

var link = $('<a>clicky clicky</a>').prop({href: dataLink, download: fileName});

var link = $('<a>').prop({href: dataLink, download: fileName , text:'clicky clicky'});

Upvotes: 2

idzignr
idzignr

Reputation: 36

Try with this following code!

<script>    
    var download = "logo11w"; /*give your file name*/
    var dataLink = "http://www.google.com/images/srpr/logo11w.png"; /*path*/
    var mylink= $("<a href='"+dataLink+"'download='"+download+"'>click</a>");
    $("#main").append(mylink);
</script>
<div id="main"></div>

Upvotes: 0

Related Questions