0

I use ajax creat html, like this

html += "<div class=\"cloumns rentprice\">" + data[i].car_price + "</div>";

and I want to catch the json's value use for index form this Dynamic html,

then sorting,

my sorting code like this

$("#rentPrice").click(function(){
        var $this=$(this);
        $("#rentPrice").not($this)
            if ($this.hasClass("up")){
                $this.removeClass().addClass("down");
            }else{
                $this.removeClass().addClass("up");
            }
            var index = $(".rentprice").text();
            var direction =$this.attr("class");
            sort(index,direction);
            show(json);
    });


    function sort(index, direction){
        json.sort(function(a,b){
            var keys = Object.keys(a);
            if (direction == "up"){
                if (a[keys[index]] > b[keys[index]]){
                    return 1;
                }else if (a[keys[index]] < b[keys[index]]){
                    return -1;
                }else{
                    return 0;
                }
            }else{
                if (a[keys[index]] < b[keys[index]]){
                    return 1;
                }else if (a[keys[index]] > b[keys[index]]){
                    return -1;
                }else{
                    return 0;
                }
            }
        });
    }
});

, but the answer is wrong , because var index = $(".rentprice").text(); can't get data[i].car_price for index,

I don't know how to get data[i].car_price form html , How can I do ?Have any idea?

steven
  • 45
  • 7
  • Check this link :http://stackoverflow.com/questions/4222690/sorting-a-json-object-in-javascript – ganesh Jan 16 '15 at 08:09

1 Answers1

0

This code gets the first item ( eq(0) ) with "rentprice" class:

$(".rentprice").eq(0).text()

This code do something with each item:

$.each($(".rentprice"), function(){
    alert($(this).text());
});

I recomend you to use libraries for work with data visualisation like knockoutjs.com. It make a better and more stable solution for data sorting and editing