tags:

views:

1451

answers:

2

See Edit Below. I am trying to get some jquery to work in a wordpress plugin I am building. (string 'jQuery' is used instead of the '$' idiom when using jquery in wordpress, fyi)

sample xml:

   <person>
        <Name>Some Name</Name>
        <location>
            <locationName>One Address</locationName>
        </location>
        <date>
            <startDate>01-01-09</startDate>
        </date>
    </person>

sample jquery:

jQuery(text).find("person").each(function(){
    jQuery("#active_list")
        .append(
            "<div id=\'Entry\'>"
            + jQuery(this).find("Name").text()
            + "<b> at </b>"
    ;

    jQuery(this)
        .find("location")
        .each(function(){
            jQuery("#active_list")
                .append(
                    jQuery(this).find("locationName").text()
                    + " <b> on </b>"
                )
            ;
        })
    ;

    jQuery("#active_list")
        .append(
            jQuery(this).find("date").find("startDate").text()
            + "</div>"
        )
    ;
});

and then the bad mark up produced:

<div id="Entry"> Some Name<b> at </b></div>One Address <b> on </b>01-01-09

as you can see it is inserting /divs right after it exits the second nested loop. I'm obviously doing something wrong but I don't know what. Any ideas?

EDIT: If put

jQuery("#active_list").append("<div id=\'ActivityEntry\'>");

on it's own line, it closes the div immediately afterwards. So I am guessing I need to built a div element with jquery and then pack it and then append my div element.

+1  A: 

my suggestion to your problem

jQuery(text).find("person").each(function(){
   var html;

   html = jQuery(this).find("Name").text() + "<b> at </b>";

   jQuery(this).find("location").each(function(){
      html += jQuery(this).find("locationName").text() + " <b> on </b>";
   });

   html += jQuery(this).find("date").find("startDate").text();

   jQuery("#active_list").append("<div id=\'Entry\'>" + html + "</div>");

});

this should work. you try to fine tune it. the reason your script didn't work was because jquery converts every string into an object.

poh
Hmm, this worked great, but it doesn't seem very jquery'ish.
Dan.StackOverflow
+2  A: 

A little tip for using jQuery with Wordpress: enclose it in a function. It'll allow you to use the familiar $ function and it'll prevent you from polluting the global namespace with variables.

For example, your code could be rewritten as:

(function($) {

    ... (code with $) ...

})(jQuery);

As for an answer to your question, nested eaches don't work in jQuery if you use the this variable. To fix your problem, try using the full style of each:

$(...).each(function(i, val1) { 

    $(...).each(function(j, val2) { ... }

});

And use val1 and val2 instead of this.

cdmckay
I modified my code to what you suggested, with the same behavior. I think what poh said about jquery turning everything in to objects is accurate, so it found an unclosed div object and closed it for me.
Dan.StackOverflow
Yeah, my bad, I didn't read the question carefully. The first piece of advice still applies though :)
cdmckay