Sev
Sev

Reputation: 912

JQuery built anchor tag will not redirect

Here is the code I am using to build the anchor tag and content within.

$('div#right-slide').html("<a href=\"http://www.XXXXXXXXXXXX.info/limited-specials/\" ><h1 id=\"specials\">Click Here for Limited</h1></a><a href=\"http://www.XXXXXXXXXX.info/limited-specials/\" ><h1 id=\"specials1\">Time Specials</h1></a>");

I am sure there is a much better way to go about this but I am new and am just trying to get it to work right now. When I hover over the text, the bottom of the browser shows the addrress for the link but when I click, nothing happens. Can someone point me in the direction of the error? Thanks for any help.

Upvotes: 0

Views: 143

Answers (1)

klenwell
klenwell

Reputation: 7148

jQuery allows you to avoid string concatenation and do this much more cleanly. For example:

function specialsLink(text, url) {
  var $h1 = $('<h1 />').text(text);
  var $a = $('<a />').attr('href', url).html($h1);
  return $a;
}

$('div#right-slide')
  .append(specialsLink('Click Here for Limited',
    'http://example.com/limited-specials/'))
  .append(specialsLink('Time Specials',
    'http://example.com/limited-specials/'));

Fiddle: http://jsfiddle.net/klenwell/CrP3q/

Upvotes: 2

Related Questions