0

I want to assign unique id into HTML DIV dynamically created by jquery Ajax.

$.ajax({
        type: 'xxx',
        data: 'xxx',
        url: 'xxx',
        success: function(data) {

                var repeatID =1;

                $.each(data.data, function(i, v){

                    repeatID++;

                    //alert(repeatID);

                    $("#divId").append('<div class="single" id="my_test_' + repeatID + '">'
                                                    +'<div class="any_name"> '+ v.Name +' </div></div>');
                });
        error:...
});

I need to assign a unique id into class 'single'. Inside the each loop, repeatID is increasing outside append (like if I uncomment alert and there are 3 arrays returned by server then it will give alert message for three times.). But inside append it is not increasing. All the values remain the initially assigined value, 1. So, for the three DIV I got id 'my_test_1'.

I am expecting to get 'my_test_1', 'my_test_2', 'my_test_3' if there are 3 arrays. Where I am doing the wrong?

Any solution please?

2
  • 1
    repeatID should be initiated outside the ajax method. Commented Apr 24, 2013 at 7:21
  • Are you sure? It works for me: jsfiddle.net/SdyYk Commented Apr 24, 2013 at 7:25

2 Answers 2

2

You can just use the index parameter (in your case, i) as the counter. You don't need repeatID as the callback parameter in the each function comes with the functionality you desire.

$.each(data.data, function(i, v){
    $("#divId").append('<div class="single" id="my_test_' + (i+1) + '">'
                          +'<div class="any_name"> '+ v.Name +' </div></div>');
}

I've used (i+1) as you seem to want it to be 1-based.

Fiddle:
http://jsfiddle.net/3wFE4/

More info on jQuery each:
http://api.jquery.com/jQuery.each/

jQuery.each( collection, callback(indexInArray, valueOfElement) )

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

3 Comments

Hi, Jace. Thanks for your reply. But I am getting the same problem. for all the DIVs the name is 'my_test_1'. But if I make a alert before append 'alert(i);', it will make a 3 times alert 0, 1, 3! I do not understand why does 'i' not increasing inside 'append'....?
@Cart that's pretty much impossible, there's something else at play. Can you please share with me all the code in your each function?
Actually I am working on Phonegap. The process (you have provided) is working fine in web. The process is not working in phonegap. Thanks a lot for your help.
1

You can try putting the increment after the append():

var repeatID =1;
$.each(data.data, function(i, v){
  $("#divId").append('<div class="single" id="my_test_' + repeatID + '">'+
   '<div class="any_name"> '+ v.Name +' </div></div>');
   repeatID++;
});

Don't know about your issues but the second one is working absolutely fine for me.

If you want to see this in a demo fiddle here

3 Comments

Already I have tried both, but not working inside 'APPEND'. outside append everything works fine. Many thanks for your reply.
@Cart added a fiddle might be helpful for you. Worked for me
Actually I am working on Phonegap. like Jase, also your process is working fine in web. The process is not working in phonegap. Thanks a lot for your help.

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.