replaceWith doesn't work the second time
Asked Answered
A

2

8

I need to click on a list item to replace text in a div with text from a node in an xml file.

I click on a list item and store the result in a variable. I then load the xml, find the title of the node I need, and make a variable of the result. I then run an if statement that if the two variables are equal, to put the xml in a div.

The first time I click on a list item, this works. The correct item in the list is matched with the correct node in the xml and the correct text is placed in the div.

But when I then click on a different list item, the text is not replaced. An alert shows that the second click got the right information, but in the end, the div content is not replaced.

Demo here: http://mwebphoto.com/mwebphoto/html/3rdJqueryPage.html

Code here:

<body>

<div class="slideshowContainer">
<div id="slideshow"></div>
</div>

<ul id="gallery_id">
  <li id="newYork">New York</li>
  <li id="disconnection">Disconnexion</li>
  <li id="jackAtSea">Jack at Sea</li>
</ul>

<script>

  $(document).ready(function(){ 

  $("#gallery_id li").on('click', function(e) {

     var htmlTitle = (this.id);

$.ajax({
  type: "GET",
    url: "/mwebphoto/xml/albums.xml",
    dataType: "xml",
    success: function(xml) {
        $(xml).find('photoAlbum').each(function(){
            var xmlAlbum= $(this);
            var xmlTitle = $(this).find('title').text();
            var xmlEmbedCode = $(this).find('embedCode').text();
                alert ('this is html titled  ' + htmlTitle);
                alert ('this is xml titled  ' + xmlTitle);
            if(xmlTitle=htmlTitle)

             alert ('this is matched xml title' + xmlTitle);

         $("#slideshow").replaceWith(xmlTitle);

        });
    }
});
});

});

  </script>
</body>

Help is appreciated. I don't ask lightly. I've spent many hours at this and researched it every way I can think of. figure I'm missing something very simple, but just can't find it.

Anlace answered 7/8, 2013 at 15:57 Comment(2)
does xmlTitle include an element with an id of slideshow? if not, then that's why it doesn't work second time. #slideshow doesn't exist. Most likely what you really need is .html rather than replaceWith.Inellineloquent
if(xmlTitle=htmlTitle) doesn't look right. Did you mean if(xmlTitle==htmlTitle) ?Americanism
T
12

After executing the first time, $("#slideshow") doesn't exists anymore, as you're replacing the whole div with the id from the XML.

What you need is text():

$("#slideshow").text(xmlTitle);

or html() if you plan to add html:

$("#slideshow").html(xmlTitle);
Tranche answered 7/8, 2013 at 16:3 Comment(0)
S
0

I think this is your problem:

if(xmlTitle=htmlTitle)

Try changing that to

if(xmlTitle==htmlTitle)

Also, replacing replaceWith with html or text would be a good idea.

Storey answered 7/8, 2013 at 16:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.