G Gr
G Gr

Reputation: 6080

jquery append json url into href

I have a json file and I can add a link as plain text to the body of articles, but I want to add the link to the href and add hand written content in the a tag:

$('div#similarArticles').html(treeObj.root[clickedID - 1].Link);

.. how can I go about this?

    <div id="articles">
    <div id="similarArticles">
        <a href="#"></a>
    </div>
    </div>

Upvotes: 0

Views: 317

Answers (2)

Sajad Karuthedath
Sajad Karuthedath

Reputation: 15767

you can do it like this

var link = treeObj.root[clickedID - 1].Link;
$("#similarArticles").find('a').attr('href', link).text('thelinktext');

With jQuery 1.6 and above you can use:

 $("#similarArticles").find('a').prop('href', link).text('thelinktext');

Upvotes: 1

Barmar
Barmar

Reputation: 781096

You need to modify the <a>, not the <div>:

var link = treeObj.root[clickedID - 1].Link;
$("#similarArticles a").attr('href', link).text(handWrittenContent);

Upvotes: 3

Related Questions