0

I have a json response

Indicator:true
content:"<a href=somelink target="_blank">Click here to open your message</a>"

I need to convert the value of content to link. Right now it is in string. how to make it a href the actual link

Unfortunately the below code isn't working

let content = res.content
var wrapper= document.createElement('div');
wrapper.innerHTML= '<div>'+content+'</div>';
var div2 = wrapper.firstChild;
4
  • 1
    What do you mean by "not working"? Commented Jan 2, 2018 at 7:28
  • tried wrapper.innerHTML= content; ? and use that wrapper directly ? Commented Jan 2, 2018 at 7:28
  • 1
    Please show all relevant code and proper data structure. What is shown has mismatched quotes and is not runnable. See minimal reproducible example Commented Jan 2, 2018 at 7:30
  • Duplicate of Render HTML string as real HTML in a React component Commented Jan 2, 2018 at 7:31

5 Answers 5

1

You can use dangerouslySetInnerHTML to directly render HTML code in React.

createMarkup(content) {
  return {__html: content};
}

convertFn(content) {
  return <div dangerouslySetInnerHTML={createMarkup(content)} />;
}

getLink(){
   ... get json response ...
   let content = res.content;
   let linkContent = this.convertFn(content);
}

Edit: This is a React.js specific example.

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

2 Comments

@raksheetbhat Where are you passing the content inside createMarkup()?
The OP is not using React, so I think this is not relevant
1

The reason is the quotation symbols. your string ends at target. You should put the somelink and the _blank inside quotation marks different from what you put the entire content in ex:

content='<a href="somelink" target="_blank"></a>'

or

content="<a href='somelink' target='_blank'></a>"

EDIT: some people have misunderstood me as to saying its because he didn't put quotes outside of somelink. its not that. look at target. why is _blank black while the rest of content is reddish? It's the type of quotes used that matter here.

11 Comments

No, Thats not the point.. the link quotation has nothing to do.
it does, after all the string actually ends at target and resumes after blank so the part in between is treated as not string and that causes the problems
the string is long.. for that sake, i've put it short
so how does the quototion marks affect the length? were u thinking I was talking about you putting somelink instead of the actual link? im not please read the answer again
somelink is dummy text i've put.. which has the essential link with quotes.. so this is out of context
|
1

use dangerouslySetInnerHtml prop to convert any html stored inside string into actual html markup.

for e.g.

htmlToText(content) {
  return {__html: content};
}

render() {
    return (
        <div dangerouslySetInnertml={this.htmlToText(yourHtmlString)} />
    )
}

Comments

-1

or can be escaped

content = "<a href=\"somelink\" target=\"_blank\">Click here to open your message</a>"

please install Prettier for editor help.

1 Comment

This is not an answer to the question, it should be a comment
-1

I find your code pretty working, As in the following snippet.

let content = '<a href=somelink target="_blank">Click here to open your message</a>';
var wrapper = document.createElement('div');
wrapper.innerHTML = '<div>' + content + '</div>'; //No need to put in div
let div2 = wrapper.firstChild;
let a = div2.firstChild;
console.log(a.innerText)
console.log(a.getAttribute('href'), a.getAttribute('target'));

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.