I've been working on a project that some other developers had started and I'm trying to understand the code while also completing the project. Currently what I have is some json with links and a url text (pretty much a quick description), what I need to do is on a button click I want to display each of the links with their correct text and make it a clickable link. The way this needs to be done is using nodes which I'm not 100% knowledgeable on. If I need to explain this more please let me know also I have provided an example of what I'm currently working with. Thanks
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>JavaScript And JSON</title> </head> <body bgcolor='#CED3F3'> <button onclick="appendUrl()">Append</button><br> <br><script id = "i"> function category() //adding function with button click removes html page styling? { var category = { "content": [ { "links": "basic information", "urlText": "Basis Information System", "urlDesc": "portal for technology development, people search, a keyword search ." }, { "links": "http://site.com", "urlText": "Net", "urlDesc": "the entry page example" }, { "links": "http://newsgroups.com", "urlText": "Newsgroups", "urlDesc": "information internal newsgroups, usage, tools, statistics, netiquette" }, { "links": "http://site2.com", "urlText": "Another site", "urlDesc": "community for transfer of knowledge and technical information" }, { "links": "http://news.com", "urlText": " some news", "urlDesc": "place with news" } ] } </script> <script> function appendUrl() { //there needs to be a loop here? var link = "needs to loop through links?" var node=document.createElement("LI"); var nodeA=document.createElement("A"); var textnode=document.createTextNode(); node.appendChild(nodeA); nodeA.appendChild(textnode); nodeA.href=(link); nodeA.target="_blank"; document.getElementById("i").appendChild(node); } </script> </body> </html>
categorydoes nothing, it should at least return your data; (2) on the other function, yes, you should be looping your data object; and (3), don't append html into a<script>element, append to a ul where list items are valid children.