0

xml file:-

<countries>
  <country code='af' phoneCode='93' name='Afghanistan' />
  <country code='al' phoneCode='355' name='Albania' />
  <country code='dz' phoneCode='213' name='Algeria' />
  <country code='ad' phoneCode='376' name='Andorra' />
  <country code='ao' phoneCode='244' name='Angola' />
  <country code='aq' phoneCode='672' name='Antarctica' />
  <country code='ar' phoneCode='54' name='Argentina' />
  <country code='am' phoneCode='374' name='Armenia' />
  <country code='aw' phoneCode='297' name='India' />
  <country code='au' phoneCode='61' name='Australia' />
</countries>

this is my JavaScript ajax:-

<script>
    var getr='';
    var ind= "India";
    var getre;
        function cli(){
            $.ajax({ 
                url: 'country_code.xml', 
                type:"get",
                async: false,
                success: function(xml) { 
                       var result = $(xml).find("countries").each(function(){
                           getr =  $(this).find("country").attr('name');
                           });
                        alert(getr);
                },
                error:function(){
                    alert('err');
                }
            });
        }
    </script>

here i want to search attribute India .
i am try above but its give me first Afghanistan
how can i do this.
thanks.

1
  • you're iterating over each country node and systematically overwriting the getr variable. First one is Afghanistan, beyond the loop the value will be Australia. I don't get what you're trying to do. If the value of getris India, you want to return India..? Commented May 8, 2013 at 9:01

1 Answer 1

2

Perhaps something like this is what you are trying to do:
Searching the country entry wiht attribute name is equal to var ind. In this example the phoneCode will be returned.

success: function(xml) { 
               var result = $(xml).find("country").each(function(){
                   if( $(this).attr('name') == ind) {
                        getr = ind + " :" + $(this).attr('phoneCode') 
                   }
                   });
                alert(getr);
        },
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.