jquery debug anyone?

  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

here's my code:

JAVASCRIPT Code: [ Select ]
$.ajax({
   type: "GET", url: "js/img.xml", dataType: "xml",error: function(){alert('Error loading XML document');},
   success:
      function(xml) {
         $(xml).find('#web link').each(
            function(){
               var indexnum = $(xml).find('image').index(this);
               var source = $(this).text();
               $("#imagecontainer").append('<img id="' + indexnum +'" src="' + source + '" />');
            });
      }
});
 
  1. $.ajax({
  2.    type: "GET", url: "js/img.xml", dataType: "xml",error: function(){alert('Error loading XML document');},
  3.    success:
  4.       function(xml) {
  5.          $(xml).find('#web link').each(
  6.             function(){
  7.                var indexnum = $(xml).find('image').index(this);
  8.                var source = $(this).text();
  9.                $("#imagecontainer").append('<img id="' + indexnum +'" src="' + source + '" />');
  10.             });
  11.       }
  12. });
  13.  


Where I want the index number to be the id of the appended tag, it's returning "-1" for all of them. Been screwing with it for most of the afternoon now, still no luck
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

Quote:
Where I want the index number to be the id of the appended tag


Can you phrase that another way? I'm not understanding the goal.
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

there's 4 items in my xml. My code generates 4 img tags in my html and gives them an id equivalent to their index in the array created from the ajax call.

So, ajax gets the xml document, then searches for the "link" tag. there are four "link" tags, therefore there are now 4 items in the array. I want the id of the generated img tags to be the same as the numerical value of the item in the array. The output should look like this:

HTML Code: [ Select ]
<img id="0" src="image.jpg" />
<img id="1" src="otherimage.jpg" />
<img id="2" src="anotherimage.jpg" />
<img id="3" src="yetanotherimage.jpg" />
 
  1. <img id="0" src="image.jpg" />
  2. <img id="1" src="otherimage.jpg" />
  3. <img id="2" src="anotherimage.jpg" />
  4. <img id="3" src="yetanotherimage.jpg" />
  5.  


The id is the number of thtag within my xml that hold the link to the image. Instead of it outputting that, I'm instead getting this:

HTML Code: [ Select ]
<img id="-1" src="image.jpg" />
<img id="-1" src="otherimage.jpg" />
<img id="-1" src="anotherimage.jpg" />
<img id="-1" src="yetanotherimage.jpg" />
 
  1. <img id="-1" src="image.jpg" />
  2. <img id="-1" src="otherimage.jpg" />
  3. <img id="-1" src="anotherimage.jpg" />
  4. <img id="-1" src="yetanotherimage.jpg" />
  5.  


all of the id's are "-1"

my xml file for this example:
XML Code: [ Select ]
   <category id="web">
      <image/>
      <link>image.jpg</link>
     
      <image/>
      <link>otherimage.jpg</link>
     
      <image/>
      <link>anotherimage.jpg</link>
     
      <image/>
      <link>yetanotherimage.jpg</link>
     
   </category>
 
  1.    <category id="web">
  2.       <image/>
  3.       <link>image.jpg</link>
  4.      
  5.       <image/>
  6.       <link>otherimage.jpg</link>
  7.      
  8.       <image/>
  9.       <link>anotherimage.jpg</link>
  10.      
  11.       <image/>
  12.       <link>yetanotherimage.jpg</link>
  13.      
  14.    </category>
  15.  
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

I haven't done a lot of XML parsing so I'm not sure why it can't find the image tags, but that appears to be the case:

Quote:
index( Element subject ) returns Number
Searches every matched element for the object and returns the index of the element, if found, starting with zero.
Returns -1 if the object wasn't found.


Perhaps you need to investigate the structure of your XML doc?
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

okay, I haven't screwed with the url to my xml and suddenly it won't load. What are the different reasons it wouldn't load?

my code:

JAVASCRIPT Code: [ Select ]
$.ajax({
      type: "GET", url: "js/img3.xml", dataType: "xml",error: function(){alert('Error loading XML document');},
      success:
         function(xml) {
            $(xml).find('#web image').each(
               function(){
                  var indexnum = $(xml).find('image').index(this);
                  var source = $(xml).find('link').text();
                  $("#webthumblist").append('<li><a href="#" id="' + indexnum +'" ></a></li>');
                  $("#imagecontainer").append('<img id="' + indexnum +'" src="' + source + '" />');
               });
         }
   });
 
  1. $.ajax({
  2.       type: "GET", url: "js/img3.xml", dataType: "xml",error: function(){alert('Error loading XML document');},
  3.       success:
  4.          function(xml) {
  5.             $(xml).find('#web image').each(
  6.                function(){
  7.                   var indexnum = $(xml).find('image').index(this);
  8.                   var source = $(xml).find('link').text();
  9.                   $("#webthumblist").append('<li><a href="#" id="' + indexnum +'" ></a></li>');
  10.                   $("#imagecontainer").append('<img id="' + indexnum +'" src="' + source + '" />');
  11.                });
  12.          }
  13.    });
  14.  


my xml file is in "root > js > img3.xml"

I'm pretty sure it's not the url because I've tried just about everything for that. "../js/img3.xml", "img3.xml", "/js/img3.xml", "js/img3.xml", "../img3.xml"
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

okay, got it...been screwing with it since last night believing it was my jquery when in fact it was my xml document, I had a tag that hadn't been closed.
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

That'll do it every time. :) IMHO, you should find yourself a good online validator and add it to your arsenal - it can really aid in avoiding this type of situation and pinpoint the XML structure as the problem up front.
  • mindfullsilence
  • Professor
  • Professor
  • User avatar
  • Posts: 854

Post 3+ Months Ago

know of one you recommend?
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

I prefer this one because it A) allows for URL, copy/paste, or file upload, B) supports external and/or additional XSD's , and C) has a pretty nice interface for showing issues once an error is detected.

Post Information

  • Total Posts in this topic: 9 posts
  • Users browsing this forum: No registered users and 90 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.