How to get the child nodes of a parent and process it with jquery ajax

Delali

I have this xml document:

<?xml version="1.0" encoding="utf-8" ?>
<modules>
    <sect>
        <mod name="Weather">
            <minWidth>200</minWidth>
            <minHeight>200</minHeight>
        </mod>
        <mod name="Settings">
            <minWidth>200</minWidth>
            <minHeight>30</minHeight>
        </mod>
    </sect>
    <sect>
        <mod name="Social">
            <minWidth>200</minWidth>
            <minHeight>200</minHeight>
        </mod>
    </sect>
    <sect>
        <mod name="Show">
            <minWidth>200</minWidth>
            <minHeight>200</minHeight>
        </mod>
        <mod name="Puzzle">
            <minWidth>200</minWidth>
            <minHeight>200</minHeight>
        </mod>
    </sect>
</modules>

and I want to display every sect node with it's child nodes as list elements with jquery ajax. I tried the following js code:

$.ajax({
        type: "GET",
        url: "/style/modules/start_page_modules.xml",
        dataType: "xml",
        success: function(xml){
        $(xml).find('sect').each(function(){
            $("<div></div>").html(function(){
            $(this).find("mod").each(function(){
                var mWidth = $(this).children("mod").find("minWidth").text();
                var mHeight = $(this).children("mod").find("minHeight").text();
                $("<li></li>").css({
                    width: mWidth,
                    height: mHeight
                }).appendTo("sect");
            });
            }).appendTo("#main_content ul");
        });
      },
      error: function() {
        alert("An error occurred while processing XML file.");
      }
      });

and all it does is to append the three sect nodes to the ul element and nothing else. I want the mod nodes to be in their respective divs like so:

<div id="sect">
  <li style="width: 200px;height:200px;"></li>
  <li style="width: 200px;height:30px;"></li>
</div>
<div id="sect">
  <li style="width: 200px;height:200px;"></li>
</div> .....and so on

Please i need help...

undefined
  1. IDs are supposed to be unique, you should use classes instead.
  2. each returns the current set, not the elements that have been generated in it's callback.
  3. You are trying to append the li elements to a non-list element which is an invalid markup.

    $(xml).find('sect').each(function (i, sect) {
        $("<ul class='sect'></ul>").html(function () {
            // `this` here refers to the `ul` ^
            return $(sect).find("mod").map(function () {
                var $mod = $(this);
                var mWidth = $mod.find("minWidth").text();
                var mHeight = $mod.find("minHeight").text();
                return $("<li></li>").css({
                    width: mWidth,
                    height: mHeight
                }); // .text(this.getAttribute('name'));
            }).get();
        }).appendTo("#target");
    });
    

The above snippet generates this structure:

<ul class="sect">
     <li style="width: 200px; height: 200px;"></li>
     <li style="width: 200px; height: 30px;"></li>
</ul>
...

http://jsfiddle.net/82ggy/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get the child nodes of a parent and process it with jquery ajax

From Dev

How to get selected child nodes as well as the parent node in jquery?

From Java

How to get child process from parent process

From Dev

how to get the list of child nodes for each treeview parent

From Dev

How to get child element of another child of the same parent with jQuery/JS

From Dev

JQuery Parent/Child Ajax load

From Dev

Using Debugger how to get child process's PID from Parent

From Dev

After starting process, how to get parent's PID in the child?

From Dev

How to get the id of a very short child process if the parent is known?

From Dev

Using Debugger how to get child process's PID from Parent

From Dev

If you fork() and exec() from the child process, and wait in the parent, how does the parent get a return code from the child?

From Dev

How to know if a process is a parent or a child

From Dev

JsTree using AJAX call to get child nodes

From Dev

JsTree using AJAX call to get child nodes

From Dev

How to get a Child Nodes Attribute

From Dev

XPATH: how to get child nodes

From Dev

How to get list of child nodes?

From Dev

Get child element of parent with jquery

From Dev

Get child list of parent process in C

From Dev

How to get a specific child divs value from parent using jquery

From Dev

How to get parent as well as child's html in jquery

From Dev

How to debug child and parent process using windbg?

From Dev

How to wait for parent process to exit in a child?

From Dev

Get child process operation result from parent process

From Dev

Parse XML: Get child nodes for each parent node

From Dev

Parse XML: Get child nodes for each parent node

From Dev

VirtualStringTree - Can't get parent-child nodes to work correctly

From Dev

How can child kill parent process while parent process waits until child process to terminate?

From Dev

How to get all child process's PIDs when given the parent PID in C

Related Related

  1. 1

    How to get the child nodes of a parent and process it with jquery ajax

  2. 2

    How to get selected child nodes as well as the parent node in jquery?

  3. 3

    How to get child process from parent process

  4. 4

    how to get the list of child nodes for each treeview parent

  5. 5

    How to get child element of another child of the same parent with jQuery/JS

  6. 6

    JQuery Parent/Child Ajax load

  7. 7

    Using Debugger how to get child process's PID from Parent

  8. 8

    After starting process, how to get parent's PID in the child?

  9. 9

    How to get the id of a very short child process if the parent is known?

  10. 10

    Using Debugger how to get child process's PID from Parent

  11. 11

    If you fork() and exec() from the child process, and wait in the parent, how does the parent get a return code from the child?

  12. 12

    How to know if a process is a parent or a child

  13. 13

    JsTree using AJAX call to get child nodes

  14. 14

    JsTree using AJAX call to get child nodes

  15. 15

    How to get a Child Nodes Attribute

  16. 16

    XPATH: how to get child nodes

  17. 17

    How to get list of child nodes?

  18. 18

    Get child element of parent with jquery

  19. 19

    Get child list of parent process in C

  20. 20

    How to get a specific child divs value from parent using jquery

  21. 21

    How to get parent as well as child's html in jquery

  22. 22

    How to debug child and parent process using windbg?

  23. 23

    How to wait for parent process to exit in a child?

  24. 24

    Get child process operation result from parent process

  25. 25

    Parse XML: Get child nodes for each parent node

  26. 26

    Parse XML: Get child nodes for each parent node

  27. 27

    VirtualStringTree - Can't get parent-child nodes to work correctly

  28. 28

    How can child kill parent process while parent process waits until child process to terminate?

  29. 29

    How to get all child process's PIDs when given the parent PID in C

HotTag

Archive