0

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>
5
  • what about $('div#similarArticles').find('a').attr('href',treeObj.root[clickedID - 1].Link); Commented Mar 3, 2015 at 4:55
  • hmm doesn't show anything Commented Mar 3, 2015 at 4:57
  • Doesn't show because you need to inspect element the a tag to see the dom attribute. Commented Mar 3, 2015 at 4:59
  • are you trying to insert content into the anchor tag as well as set the value of the href? Commented Mar 3, 2015 at 4:59
  • yes I am, forgot @NorlihazmeyGhazali method would be blank Commented Mar 3, 2015 at 5:02

2 Answers 2

3

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

var link = treeObj.root[clickedID - 1].Link;
$("#similarArticles a").attr('href', link).text(handWrittenContent);
Sign up to request clarification or add additional context in comments.

Comments

1

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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.