0

i am getting data from XML into html using jquery, i build a function which is working perfectly which is given below

function parseXml(xml){
    xmlcontent = xml;
    $('#file').html('$'+ GetDeals('delhi','india'));
    $('#file1').html('$'+ GetDeals('mumbai','india'));
    $('#file2').html('$'+ GetDeals('kolkata','india'));    
}

function GetDeals(state, country){
    var lowestPrice=0;    
    $(xmlcontent).find("ndata[nstate='"+state+"'][ncountry='"+ country +"']").each(function(){
        lowestPrice =  parseInt($(this).attr('price')) ;
    });
    return lowestPrice;    
}

now i want pick pick one more field call cityrating from xml for example first parameter contains cityrating= 1, and second cityrating=2, and last one is 3 and i wana get it in new div, i struggle with it almost 3 hour but it is not working my change function given below. when i am running this function cityrating replace by last cityrating for example all parameter cityrating replaced by 3

function parseXml(xml){
    xmlcontent = xml;
    $('#file').html('$'+ GetDeals('delhi','india'));
    $('#file1').html('$'+ GetDeals('mumbai','india'));
    $('#file2').html('$'+ GetDeals('kolkata','india'));    
}

function GetDeals(state, country){
    var lowestPrice=0;
    var cityRating=[];

    $(xmlcontent).find("ndata[nstate='"+state+"'][ncountry='"+ country +"']").each(function(){
        lowestPrice =  parseInt($(this).attr('price'));
        cityRating.push(parseInt($(this).attr('CityRating')));
        $('.city').html(cityRating[0])
    });
    return lowestPrice;    
}

//my html

<div id="file"></div>
<div class="city"></div>

<div id="file1"></div>
<div class="city"></div>

<div id="file2"></div>
<div class="city"></div>
2
  • when i alert(cityRating) it is showing correct value in alert box but it is showing one by one like first time 1 then 2 and 3 .push method not joining them together Commented Apr 21, 2012 at 12:29
  • method to call xml is $(function() { $.ajax({ type: "GET", url: "newdata.xml", dataType: "xml", success: parseXml }); }); Commented Apr 21, 2012 at 12:51

1 Answer 1

2

I see that you are trying to find an element with class city which does not exist.

It should be $('#city').html(cityRating[0]) instead $('.city').html(cityRating[0])

Sign up to request clarification or add additional context in comments.

8 Comments

@amit can you provide your sample xml. I can use it in jsfiddle
<ndata nstate="delhi" ncountry="india" price="200" cityRating="1" /> <ndata nstate="mumbai" ncountry="india" price="300" cityRating="2" /> <ndata nstate="kolkata" ncountry="india" price="400" cityRating="3" />
I see one more typo. It should be parseInt($(this).attr('cityRating')) instead parseInt($(this).attr('CityRating'))
@amit Also im not sure what result you want. Because when you do $('.city').html(cityRating[0]), rating will be changed for all the matched divs with class city. If you want corresponding city rating to be displayed, you need to send corresponding div ids to GetDeals function
i want that i have three div which have class city in first city div i want rating 1, in second city div i want rating 2 and in last city div i want cityrating 3, but when i am running this function all the city div appending with cityrating= 3
|

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.