Jack Php
Jack Php

Reputation: 577

find xml attribute value using variable name

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.

Upvotes: 0

Views: 595

Answers (1)

hr_117
hr_117

Reputation: 9627

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);
        },

Upvotes: 2

Related Questions