0

I'm practicing Ajax and using JavaScript to handle response of my data. I’m trying to do like a Facebook comment where you can edit your comment if you’re the one who made it. here instead of word "edit" I put a counter number just to see if comment will have their counter number correctly, luckily everything works fine so I put a code where if you click that counter number it will alert its counter number, that’s where my problem comes from because when I click the counter no. "0" it should alert also "0" but its alert different no.

Any Help would be appreciated.

function handleResponse(){
    var xmlResponse = xmlHttp.responseXML;
    root = xmlResponse.documentElement;
    edit = root.getElementsByTagName("h4");
    counter = root.getElementsByTagName("h3");
    user = root.getElementsByTagName("h1");
    comment = root.getElementsByTagName("h2");
    var counter2 = 0;
    var user1 = "";
    var comment1 = "";
    var edit1 = "";

    for(var i=0;i<counter.length;i++) {
        var counter1 = counter.item(i).firstChild.data;
    }

    while(counter1>=counter2){
        user1 = user.item(counter2).firstChild.data;
        comment1 = comment.item(counter2).firstChild.data;
        edit1 = edit.item(counter2).firstChild.data;

        if( $('.userclass'+ counter2).length ) {
            $('.userclass'+ counter2).text(user1);
            $('.commentclass'+ counter2).text(comment1); 
            if(edit1 == 1){
                $('.editclass'+ counter2).text(counter2);
            } 
        }else{
            $('<div id="DivUser"> </div>').insertBefore("#starting").addClass('userclass'+ counter2);
            $('.userclass'+ counter2).text(user1);
            $('<div id="DivComment"> </div>').insertAfter('.userclass'+ counter2).addClass('commentclass'+ counter2);
            $('.commentclass'+ counter2).text(comment1);
            if( edit1 == 1 ){
                $('<div id="DivEdit"> </div>').insertAfter('.commentclass'+ counter2).addClass('editclass'+ counter2);
                $('.editclass'+ counter2).text(counter2); //here instead of word "edit" i use the variable counter to see if everything goes ok, and everything is ok here.. 
                //return counter2;
                $( ".editclass"+ counter2 ).click(function() {
                  alert( counter2 ); // here where my problem start it alert different number EX: i click 0 but it alert diff no.
                });
            } 
        }
    counter2++;     
    }// end of while
}// end of handleResponse
Prabhat Sinha
  • 1,500
  • 20
  • 32

0 Answers0