Skip to content Skip to sidebar Skip to footer

Creating An Href On Returned JSON Data

I have some basic JSON data returned from an ajax request which populates a table JSON {'data': { 'id': '100', 'name': 'file name', 'url': 'www.somesite.com/abc

Solution 1:

Every single AJAX request is asynchronous, so there is no other option but to handle it in a callback (traditional or wrapped in promise).

This is how I would do it:

// Get data is some function that makes the request and accepts a callback...
getData(function(data){
    // Build an anchor tag...
    var link = document.createElement('a');
    link.setAttribute('href', data.url);
    link.innerHTML = 'Click Here!';

    // Add it to the element on the page.
    var table = document.getElementById("table");
    table.appendChild(aTag);
});

Make sure you modify the code to add it to the page, it will be different based on your markup.

Inspired By: How to add anchor tags dynamically to a div in Javascript?


Post a Comment for "Creating An Href On Returned JSON Data"