Problem by loading lightbox with ajax
Content of File B is loaded by AJAX in File A. In the head of File A, there is the link for lightbox.js. The elements adressed to the lightbox are loaded with AJAX afterwards from the File B.
The problem: The lightbox isn't recognizing the loaded elements. Because I dont have much AJAX and jQuery experience, I don't understand most of the solutions I found on the web. Or I just can't use them right.
- - File A contains lightbox.js
- - File B contains the images for lightbox.js
- - AJAX loads the images from File B into File A
- - By click on the images,the lightbox code isn't working
File A
- <head>
- <title>Batman</title>
- <link rel="stylesheet" type="text/css" href="../css/batman.css" media="only screen" />
- <link rel="stylesheet" type="text/css" href="../css/jquery.lightbox-0.5.css" media="screen" />
-
- <script language="Javascript" src="../js/ajax.js"></script>
- <script type="text/javascript" src="../js/jquery.js"></script>
- <script type="text/javascript" src="../js/jquery.lightbox-0.5.js"></script>
- <script type="text/javascript" src="../js/batman.js"></script>
- </head>
- [...]
Through the selection by the option-tags, the images are loaded:
- [...]
- <form>
- <select name="batmantype" size="1" onchange="batmanFilter(this.value)">
- <option value="" selected="selected">(Please select)</option>
- <option value="batman">Batman</option>
- <option value="robin">Robin</option>
- </select>
- <div id="batmanFilter">
- <!-- Between these div-tags, loaded by AJAX-->
- <button type="button" class="showinfo" name="loadinfo" value="<?php echo $filter0; ?>" onclick="batmanfunction(this.value)">Batman compare</button>
- </div>
- </form>
- <div class="infobatman" id="batmanEvaluate">
- <!-- Between these div-tags, lightbox images are inserted by AJAX -->
- </div>
- [...]
AJAX Code:
- [...]
- function batmanfunction(str)
- {
- if (window.XMLHttpRequest)
- {// code for IE7+, Firefox, Chrome, Opera, Safari
- xmlhttp=new XMLHttpRequest();
- }
- else
- {// code for IE6, IE5
- xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
- }
- xmlhttp.onreadystatechange=function()
- {
- if (xmlhttp.readyState==4 && xmlhttp.status==200)
- {
- document.getElementById("batmanEvaluate").innerHTML=xmlhttp.responseText;
- }
- }
- xmlhttp.open("GET","fileB.php?batValue="+str,true);
- xmlhttp.send();
- }
- [...]
Datei B:
The image-variables are all loaded correctly. So don't worry about that. The preview images are loaded by ajax, but the lightbox isn't reacting.
- [...]
- <?php
- if (!empty($image[10]))
- {
- ?>
- <a href="../img/screenshots/<?php echo $image[11];?>" title="<?php echo $image[12]; ?>" class="lightbox"><img src="../img/screenshots/<?php echo $image[10];?>" alt="<?php echo substr($image[11], 0, -4); ?>" /></a>
- <?php
- }
- ?>
- <?php
- if (!empty($image[13]))
- {
- ?>
- <a href="../img/screenshots/<?php echo $image[14];?>" title="<?php echo $image[15]; ?>" class="lightbox"><img src="../img/screenshots/<?php echo $image[13];?>" alt="<?php echo substr($image[14], 0, -4); ?>" /></a>
- <?php
- }
- ?>
- <script type="text/javascript">
- $(function() {
- $('a.lightbox').lightBox();
- });
- </script>
- </div> <!-- end of gallery-->
- [...]
I have already found many different aproaches and tried them out, but I couldn't implement them, so it works. Maybe the problem is caused by the asynchronic loading of AJAX and that's why lightbox isn't recognizing the images.
I hope you can help me there! A step by step instruction would be nice.
Greetings, Bruce