JulianJ
JulianJ

Reputation: 1309

Create a dynamic href link inside jquery

I am trying to construct a dynamic href link inside jquery but can't quite get there. I can get data-id variable into jquery but I can't figure out how to create a link. Can anyone tell me what I'm going wrong?

I'm trying to achieve this:

<a href="page.php?page_id=some_page_id>"Read More</a>

My html

    <a class="fancybox" class="img-responsive" data-id="<?php echo $page_id ?>" id="single_image" href="uploads/<?php echo $file ?>" title="<?php echo $title ?>" width="800 ">
<img src="uploads/thumbnails/<?php echo $file ?>" id="<?php echo $file ?>" class="img-responsive" width="190"></a>

Jquery

$(document).ready(function() {
$("a#single_image").fancybox({
 afterLoad: function() {
    this.outer.append("<div>" + document.getElementById("detail").innerHTML + "<a href="page.php?page_id=" +$(this.element).data('id') + "Read More</a></div>");
  }
});

});

Upvotes: 0

Views: 1230

Answers (2)

Mohit Tanwani
Mohit Tanwani

Reputation: 6638

Use single quote inside double for 'page.php?page_id='

Replace this jquery code with yours.

$(document).ready(function() {
   $("a#single_image").fancybox({
     afterLoad: function() {
       this.outer.append("<div>" + document.getElementById("detail").innerHTML + "<a href='page.php?page_id='"+$(this.element).data('id') + "Read More</a></div>");
     }
   });
});

Upvotes: 0

oxalorg
oxalorg

Reputation: 2808

You aren't escaping the inner quotes properly. Try this:

this.outer.append("<div>" + document.getElementById("detail").innerHTML + "<a href=\"page.php?page_id=\"" +$(this.element).data('id') + "Read More</a></div>");

Upvotes: 1

Related Questions