0

I have created a JSON file with various data:

[
  {
    "date": "17.06.",
    "event": "The Stoles gig",
    "url": "http://thestoles.com/"
  },
  {
    "date": "25.06.",
    "event": "The Editors release an EP",
    "url": "http://theeditors.com/"
  }
]

Everything is rendered correctly in the HTML file, except for the URL, which doesn't show as a link.

Here's my code:

$(document).ready(function() {  
    $.getJSON('feeds.json', function(data){
        $.each(data, function(i, item){
            $('#feeds').append(item['date'] + item['event'] + item['url'] + "</br>");
        });
    });
});

Any suggestions?

5
  • 4
    If you want a link, you have to create an anchor element (<a>). w3.org/TR/html4/struct/links.html (FYI, this has nothing to do with JSON, so I updated your question). Commented Jun 16, 2013 at 9:53
  • Jquery is very useful creating elements and adding them to the dom Commented Jun 16, 2013 at 9:57
  • I have your answer below.... :) Commented Jun 16, 2013 at 9:59
  • a link is an element, you need to set your url in 'href' property of an '<a href='..'> text shown </a>' in that .append Commented Jun 16, 2013 at 9:59
  • Why the downvote a valid question, just one with an easy answer to non beginnners!!! This site annoys me sometimes Commented Jun 16, 2013 at 10:06

2 Answers 2

1

You've to sourround the URL by an anchor-tag:

$(document).ready(function() {  
    $.getJSON('feeds.json', function(data){
        $.each(data, function(i, item){
            $('#feeds').append(item['date'] + item['event'] + '<a href="'+item['url']+'">Link</a></br>');
        });
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

No problem, I always hate when people point out my syntax error and fix it for me....I enjoy finding it myself, so I assume others might wanna as well....:P
1

Just do this...

You need to put the link in an <a> tag...

$(document).ready(function() {  
$.getJSON('feeds.json', function(data){
    $.each(data, function(i, item){
        $('#feeds').append(item.date + item.event + "<a href='"+item.url+"'>"+item.url+"</a></br>");
    });
   });
 });

Or if you dont want to actually display the link, and just have the Event name hyperlinked...

$(document).ready(function() {  
 $.getJSON('feeds.json', function(data){
    $.each(data, function(i, item){
        $('#feeds').append(item.date + "<a href='"+item.url+"'>"+item.event+"</a></br>");
    });
   });
 });

2 Comments

This is better answer than @Mr. Bombastic
hey guys, none of the above is working. If I hyperlink it inside the code the link just doesn't show at all

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.