Search code examples
javascriptjqueryjsonjsonp

JSON Issue with API HREF


So i'm trying to get the href code to be a clickable button. So far i'm just trying to make it as a link!

Heres the code

$("table").append("<tbody><tr><td>"+title+"</td><td><a href='&quot;+href+&quot;'></a></td></tr></tbody>");

The JSON pulled back a Href link, All I want is to have a tabled button that says link and redirects the page using the link from the json.

Sam


Solution

  • You need to do two things:

    1. create a button element
    2. Attach a click event redirecting the page
      var button = "<button class='redirect-button' data-url='"+href+"'>Link</button>";
      $("table").append("<tbody><tr><td>"+title+"</td><td>"+button+"</td></tr></tbody>");
      $("table").find(".redirect-button").click(function(){
           location.href = $(this).attr("data-url");
      });