user2381130
user2381130

Reputation: 193

select XML attribute and pop into a table

I am tring to make a button that user can select to filter the stores they want and see the result in a table. for example, select XML rows with state attribute equals to A. But nothing returned, please advise if any syntax has problems. Thanks.

<markers>
<marker name="Store A" address="1 ABC street" state="A" phone="1234567"  />
<marker name="Store B" address="2 ABC street" state="B" phone="1234567"  />
<marker name="Store C" address="3 ABC street" state="A" phone="1234567"  />

<input type='button' name='select1' value="A" onClick="changeFilter(this.value);">

function changeFilter(ctry){
$country=ctry;              
$(".datarow").remove();     
loadTable($xmldata);        
}


function loadTable(data){
$xmldata=data;                  
$(data).find("marker[state=" + ctry + "]").each(function(){ 

    var $this=$(this);                          
    var $name=$this.find('marker[name]').text();    
    var $tel=$this.find('marker[phone]').text();    
    var $country=$this.find('marker[address]').text();  

    $("#datatable").append("<tr class='datarow'><td>"+$name+"</td><td>"+$tel+"</td><td>"+$country+"</td></tr>"); 
}); 
}

Upvotes: 1

Views: 39

Answers (1)

Anoop Joshi P
Anoop Joshi P

Reputation: 25527

You need to use .attr() get method inorder to get the attribute value.

$("div").find("marker[state=" + ctry + "]").each(function(){ 
 var $this=$(this);    
    var $name=$this.attr("name");    
    var $tel=$this.attr("phone");    
    var $country=$this.attr("address");  

}); 

Fiddle

Upvotes: 1

Related Questions