-1

I have a HTML string like this test <b contact-id=dd4a1dbf-66f2-4791-87ee-f01511df888d>@Sandeep Mohan</b>. Now, I want to render that into <b> tag how I can do that? While rendering I'm getting this as a string and it is displaying in the web browser. I have an array and again I'm converting into a string. Again I want that string into HTML tag.

var fullName="<b contact-id="+index+">"+"@"+MentionsObj.fullName+"</b>";
         contacts[mentionIndex]=index.replace(index,fullName);
         }
      }
    }
contacts=contacts.toString();
contacts=contacts.replace(/,/g," ");

I'm converting the array into the string and then removing coma. Now, my return statement should return a html.

3
  • 1
    Share your code to understand this better. Commented Jun 23, 2017 at 12:55
  • 2
    Use $(...).html("<b ... </b>") instead of $(...).text("<b ... </b>"). Commented Jun 23, 2017 at 12:58
  • Not sure what you mean by "my return statement should return a[n] html" - an html what? some html? it's already some html, html is just a string. If you mean DOM element, then $(contacts) or just append() or set (.html()) where you want it. Commented Jun 23, 2017 at 13:25

3 Answers 3

0

Use the javascript function document.write(myHTMLString)

For example :

https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_doc_write

Put everything you want in place of the "Hello World!", even html tags.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to convert the string into valid HTML element first. You can use parseHTML to change the string into HTML elements:

contacts[mentionIndex]=index.replace(index,$.parseHTML(fullName));

Comments

0

To create a element that you can append to your HTML element, you can use the following code.

var b = document.createElement('b');
b.setAttribute('contact-id', 'cake');
b.innerText = '@Sandeep';
document.body.appendChild(b);

Result:

<body>
  <b contact-id="cake">@Sandeep</b>
</body> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.