Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9182807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:38:58+00:00 2026-06-17T18:38:58+00:00

this is my xml file :- <root> <child1 entity_id = 1 value= Asia> <child2

  • 0

this is my xml file :-

<root>
<child1 entity_id = "1" value= "Asia">
    <child2 entity_id = "2" value = "india">
        <child3 entity_id = "3" value = "Gujarat">
            <child5 entity_id = "5" value ="Rajkot"></child5>
        </child3>
        <child4 entity_id = "4" value = "Rajshthan">
            <child6 entity_id = "6" value = "Ajmer"></child6>
        </child4>
    </child2>
</child1>
</root>

this is my code :-

<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
<script type="text/javascript" src="jquery.js"></script>
<script>
data = false;
loaded = false;

function loadChild(id) {
    if(!loaded) {
        ul = "<ul>";
        $(data).find("[entity_id='" + id + "']").children().each(function(){
             var value_text = $(this).attr('value');
             var id = $(this).attr('entity_id');

            ul += "<li id='" + id + "'>" + value_text + "</li>";
         });
         ul += "</ul>";
         $("#" + id).append(ul);
         loaded = true;
    }
}

 $(function() {
     $('#update-target a').click(function() {
         $.ajax({
             type: "GET",
             url: "final.xml",
             dataType: "xml",
             success: function(xml) {
                data = xml;
                //console.log(data);
                 $(xml).find('child1').each(function(){
                     var value_text = $(this).attr('value');
                     var id = $(this).attr('entity_id');
                     $("<li id='" + id + "' onclick=\"loadChild('" + id + "');\"></li>")
                         .html(value_text)
                         .appendTo('#update-target ol');
                 }); //close each(
             }
         }); //close $.ajax(
     }); //close click(
 }); //close $(
</script>
   </head>
   <body>
     <p>
       <div id='update-target'>
         <a href="#">Click here to load value</a>
         <ol></ol>
       </div>
     </p>

</body>
</html>

i got the output something like this :-

Asia
India

i want something like this output:-

if click on Asia then display India
if click on India then display Gujarat and Rajshthan
if click on Gujarat then display Rajkot
if click on Rajshthan then display Ajemr

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T18:38:59+00:00Added an answer on June 17, 2026 at 6:38 pm

    Firstly, if you want to continually click on the child values, you will need to add your loadChild function as a click event on each child list item. So, instead of this line

    ul += "<li id='" + id + "'>" + value_text + "</li>";
    

    You need this line

    ul += "<li id='" + id + "' onclick=\"loadChild('" + id + "');\">" + value_text + "</li>";
    

    However, this leads on to the next issue. You are using a global variable loaded to check whether the XML has already been read. But once this has been set to true (when clicking india) then the loadChild function will not do anything else. You really need to keep a track of the loaded value for each separate child. You could make use of the jquery data function here, for example

    var obj = $("#" + id);
    if(obj.data("loaded") == null)
    {
       // Load data
    }
    obj.data("loaded", true);
    

    Note that another thing you will need to avoid is that when clicking on a child element, you want to avoid the onclick event of the parent firing (which may happen because the child element is embedded within the parent’s li) element, and so you will need to tell the browser not to bubble up the click event.

    event.cancelBubble = true;
    

    Finally, to simplify the code a bit, you could actually get the click handler for update-target a to call your loadChild function, but using a ‘dummy’ element with an id of 0.

    Try the following code instead:

    <!DOCTYPE HTML>
    <html lang="en-US">
    <head>
        <meta charset="UTF-8">
        <title></title>
    <script type="text/javascript" src="jquery.min.js"></script>
    <script>
    data = false;
    
    function loadChild(id) {
        var obj = $("#" + id);
    
        if(obj.data("loaded") == null) {
            ul = "<ul>";
            var path = (id == 0) ? "root" : "[entity_id='" + id + "']";
            $(data).find(path).children().each(function(){
                 var value_text = $(this).attr('value');
                 var id = $(this).attr('entity_id');
                 ul += "<li id='" + id + "' onclick=\"loadChild('" + id + "');\">" + value_text + "</li>";
             });
             ul += "</ul>";
    
             $("#" + id).append(ul);
             obj.data("loaded", true);
             event.cancelBubble = true;
        }
    }
    
     $(function() {
         $('#update-target a').click(function() {
             $.ajax({
                 type: "GET",
                 url: "final.xml",
                 dataType: "xml",
                 success: function(xml) {
                    data = xml;
                    loadChild("0");
                 }
             }); //close $.ajax(
         }); //close click(
     }); //close $(
    </script>
       </head>
       <body>
         <p>
           <div id='update-target'>
             <a href="#">Click here to load value</a>
             <ol id="0"></ol>
           </div>
         </p>
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets consider this xml file : <?xml version=1.0 encoding=UTF-8?> <root attribute=value> <element>myElement</element> </root> I'm
i have a xml file like <?xml version=1.0 encoding=ISO-8859-1?> <childrens> <child entity_id=1 value=Root Catalog
I have this XML at http://localhost/file.xml : <?xml version=1.0 encoding=utf-8?> <val:Root xmlns:val=http://www.hw-group.com/XMLSchema/ste/values.xsd> <Agent> <Version>2.0.3</Version>
The xml file is having the following structure <Root> <Child value=A/> <Child value=B/> <Child
I have an XML file like this to work with. <root> <ignore> <child>1</child> </ignore>
This is a sample of the XML file : <Nodes version=1> <Node name=root> <Node
With this XML file - <Instructions> <Admin> <Instruction> Steps must be completed in order.&lt;/br&gt;
This is my Xml file.i want to transform this xml file into another customized
I have this xml file which has text init. i.e Hi my name is
I got this XML file: <Msg UserText=start 0> </Msg> <Msg UserText=A> </Msg> <Msg UserText=A>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.