0
var n = [
    { id: "unep", 
    title: "UNEP", 
    body: " The United Nations Environment Programme (UN Environment) is the leading global environmental authority that sets the global environmental agenda,",
     img: "img/unep.png" 
    }

I'm trying to insert the anchor tag in the content of body but it doesn't seem to work. How can I insert the anchor tag?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Harsh Singh
  • 29
  • 1
  • 4
  • how do you use an `a` tag if you don't have any link to specify as `href` attribute in your element? – quirimmo Jan 30 '19 at 20:00
  • Possible duplicate of [How do I create a link using javascript?](https://stackoverflow.com/questions/4772774/how-do-i-create-a-link-using-javascript) – Heretic Monkey Jan 30 '19 at 20:39

1 Answers1

1

If you want a text only output, you may create a new HTML element with createElement, then add required properties, and finally output text version of you freshly created tag with .outerHTML method:

var n = {
  id: "unep",
  title: "UNEP",
  body: "The United Nations Environment Programme … ",
  img: "img/unep.png"
};

const linkTag = document.createElement('a');
linkTag.href = 'https://google.com';
linkTag.textContent = 'Link';

const linkTagAsText = linkTag.outerHTML;

n.body = n.body + linkTagAsText;
mab
  • 378
  • 1
  • 8