0

I have Script is as below

$(document).ready(function () {
    $('body').find('.tree').fadeOut(0);

   $('.tree-title').click(function () {
	   var id = $(this).attr('id');
	   $.ajax({
			contentType : "application/json; charset=utf-8",
			dataType : "json",
			url : "loadSubFolders",
			type : 'GET',
			data : {
				id : id
			},
			success : function(response) {
				$.each(response, function(index, value) {
						response.push('<li class="tree-title" id='+value.folderId+'>'+value.folderName+'</li>');
						alert("Appended Successfully");
				});
				$('.tree').append( response.join('') );
			},
			error : function(res, textStatus) {
				var msg = "Unable to load Subfolder";
				alert(msg);
			}
		});
        setStatus($(this));
    });
});

In this I want to compare data means id to some response element like

success : function(response) {
    		$.each(response, function(index, value) {
    	if(id==value.rootId){
    response.push('<li class="tree-title" id='+value.folderId+'>'+value.folderName+'</li>');
    	alert("Appended Successfully");
    	}
   else{
    response.push('<li class="tree-title" ></li>');
    alert("Append Failed");
    	}
   });
    $('.tree').append( response.join('') );
},

But it s not working

How could i achieve this? Can anybody suggest me?

3
  • What issue you are facing in this? Commented Aug 1, 2018 at 6:37
  • can you explain more specify? compare again with what? Commented Aug 1, 2018 at 6:38
  • in ajax response list is there and i have id which is ajax data so now after getting ajax response i want to check that data with response as list element Commented Aug 1, 2018 at 6:41

2 Answers 2

1

You can supply properties to your success callback and access them from within the closure using this. Example this.id The custom properties you specify can't already be defined in the ajax prototype, otherwise you'll rewrite the default values. Try this:

$(document).ready(function () {
    $('body').find('.tree').fadeOut(0);

   $('.tree-title').click(function () {
       var id = $(this).attr('id');
       $.ajax({
            contentType : "application/json; charset=utf-8",
            dataType : "json",
            url : "loadSubFolders",
            type : 'GET',
            data : {
                id : id
            },
            id : id,
            success : function(response) {
                afterSuccess(response , this.id);

                function afterSuccess(data, i) {
                    $.each(data, function(index, value) {
                        if(i==value.rootId){
                            data.push('<li class="tree-title" id='+value.folderId+'>'+value.folderName+'</li>');
                            alert("Appended Successfully");
                        }
                        else{
                            alert("Append Failed");
                        }
                    });
                    $('.tree').append( data.join('') );
                }
            }
        });
        setStatus($(this));
    });
});
Sign up to request clarification or add additional context in comments.

9 Comments

what does console.log(this.id); output when placed inside your success callback? Is it defined? Is value.rootId defined?
define alert(this.id) and works properly outside of each loop but didn't get value.rootId.
Sorry you may have to append this.id to your response object since you are using $.each to iterate over it. Or optionally create a partial function supplying your custom parameter as an argument inside your success callback and call that function within your the $.each function
Well then there is a problem with value.rootId. I would first check to see what your response is using console.log(response) then find out how to access its properties.
If I'm not mistaken, response may be returning the outer container of JSON. You may have better luck with something like $.each(response[0], function(i,val){...}) Or something along those lines.
|
0

You could load your results into a separate variable like contents

$(document).ready(function () {
    $('body').find('.tree').fadeOut(0);

   $('.tree-title').click(function () {
       var id = $(this).attr('id');
       $.ajax({
            contentType : "application/json; charset=utf-8",
            dataType : "json",
            url : "loadSubFolders",
            type : 'GET',
            data : {
                id : id
            },
            id : id,
            success : function(response) {
                afterSuccess(response , this.id);
                function afterSuccess(data, i) {
                    var contents = "";
                    $.each(data, function(index, value) {
                        if(i==value.rootId){
                            contents += '<li class="tree-title" id='+value.folderId+'>'+value.folderName+'</li>';
                            alert("Appended Successfully");
                        }
                        else{
                            alert("Append Failed");
                        }
                    });
                    $('.tree').append( contents );
                }
            }
        });
        setStatus($(this));
    });
});

Comments

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.