-2

I am getting a file name on image click like this

<a href="#"  class="aclick" >
   <img data-link="petroliumjelly.php" src="productsimgs/petroliumjelly.png" class="img-responsive" id="2">
</a>

Now there pops up a modal window here and i have the page link petroliumjelly.php that i want to open in that modal window .

  <script type="text/javascript" language="javascript">
       $(".aclick").click(function (e) {
        e.preventDefault();
        var id = $(this).attr('id');
        var link = $(this).data("data-link");
        console.log(id);
        console.log(link);
        $('#modal_'+id).modal('show');
        $page = $(this).find('img').attr("data-link"); 
        //$('.modal-body')// load page her 
    });

</script> 

How can i load that page in modal window please help .

drmonkeyninja
  • 8,490
  • 4
  • 31
  • 59
Sikander
  • 2,799
  • 12
  • 48
  • 100

1 Answers1

1

The .aclick doesn't have an id and data-link, they will return undefined, you need to find the image and get those from the image. To get the data-link use ().data('link') not data('data-link'). To load the file in your modal you'll have to use AJAX or the .load() to laod a file.

<script>
    $(".aclick").click(function (e) {
        e.preventDefault();
        $image = $(this).find('img');
        var id = $($image).attr('id');
        var link = $($image).data("link");
        console.log(id);
        console.log(link);
        $('#modal_'+id).modal('show');

        // using AJAX to fetch the file
        $.get(link, function (response) {
            $('.modal-body').html(response);
        })
    });
</script>

Or use modal.load() to load the file.

<script>
    $(".aclick").click(function (e) {
        e.preventDefault();
        $image = $(this).find('img');
        var id = $($image).attr('id');
        var link = $($image).data("link");
        console.log(id);
        console.log(link);

        // load the file
        $('.modal-body').load(link, function () {
            $('#modal_'+id).modal('show');
        })
    });
</script>
Junius L
  • 15,881
  • 6
  • 52
  • 96