0

i have a xml file like

<?xml version="1.0" encoding="ISO-8859-1"?>
<childrens>
<child entity_id="1" value="Root Catalog" parent_id="0">
<child entity_id="2" value="Apparel" parent_id="1">
    <child entity_id="4" value="Shirts" parent_id="2"/>
    <child entity_id="5" value="Pants" parent_id="2"/>
</child>
<child entity_id="3" value="Accessories" parent_id="1">
    <child entity_id="6" value="Handbags" parent_id="3"/>
    <child entity_id="7" value="Jewelry" parent_id="3"/>
</child>
</child>
</childrens>

n have tried to get data where parent_id=2 n have written this jquery code

$(document).ready(function(){
        $.ajax({
            type: "GET",
            url: "test.xml",
            dataType: "xml",
            success: function(xml) {
                $(xml).find('child').attr('[parent_id=3]').each(function(){
                    var id = $(this).attr('entity_id');
                    alert(id);

                });
            }
        });
    });

but it is not working

if i remove .attr('[parent_id=3]') then each id will be alert

1 Answer 1

1

Wrong use of attr, the way you used attr('[parent_id=3]') will search attributes with name=[parent_id=3] and if element has such attributes then it string value will be used by each which seems not required here.

Live Demo

Change

$(xml).find('child').attr('[parent_id=3]').each(function(){

To

$(xml).find('child[parent_id=3]').each(function(){
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.