Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

get a javascript object as a link

I need your help; i want the name obtained from the json array to be a link; that is, once I get the name I click on it to be directed to a page (for example, it is assumed www.google.com).
To do this I used the following line:

document.getElementById("demo").innerHTML = "+myObj["name"]+";

but I get nothing. Can anyone help me please?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const myJSON = '{"name":"John", "age":30, "car":null}';
const myObj = JSON.parse(myJSON);
document.getElementById("demo").innerHTML = "<a href='http://www.google.com'>+myObj["
name "]+</a>";
<h2>Access a JavaScript Object</h2>
<p id="demo"></p>

>Solution :

document.getElementById("demo").innerHTML = "<a href='http://www.google.com'>"+myObj['name']+"</a>";

or

document.getElementById("demo").innerHTML = `<a href='http://www.google.com'>${myObj["name"]}</a>`;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading