0

I have $each loop in jquery as below

some code---------
var temp="";
var msg_html="";

$.each(_d, function(index, val) {

        var currentID=val.id;

        if(temp.match(new RegExp("(?:^|,)"+currentID+"(?:,|$)"))) {

        msg_html+'_'+currentID        += messageTemplate(val.photo, val.from_name, val.message);

        } else{

        msg_html+'_'+currentID        += messageTemplate(val.photo, val.from_name, val.message);

        temp += currentID + ",";
        }

        });


some code ------------------

If the above code works, I can display as like,

        $('#someID_0').html(msg_html_0);
        $('#someID_1').html(msg_html_1);
        $('#someID_2').html(msg_html_2);
        ----
        ---
        ---

The problem in this code is that some error in "msg_html+'_'+currentID "as invalid assignment. Any other way to assign dynamically?

5
  • Any reason you can't do $('#someID_' + currentId).html(/** html here **/) inside the loop instead? Commented Apr 30, 2013 at 5:26
  • 1
    Try msg_html = msg_html + "_" + currentID + messageTemplate(val.photo, val.from_name, val.message); Commented Apr 30, 2013 at 5:29
  • the result in $.each should be combined by "+=" Commented Apr 30, 2013 at 5:29
  • hi Dineshkani, by how to display $('#someID_' + currentId).html(msg_html_0). Commented Apr 30, 2013 at 5:31
  • Couldn't you use $('#someID_' + currentId).append(messageTemplate(val.photo, val.from_name, val.message)) inside the .each() loop? Commented Apr 30, 2013 at 21:36

4 Answers 4

2

Instead of trying to assign different variables, why not use an associative array instead with the keys of array being the IDs?

Hence your code would be:

some code---------
var temp="";
var msg_html={};

$.each(_d, function(index, val) {

        var currentID=val.id;

        if(temp.match(new RegExp("(?:^|,)"+currentID+"(?:,|$)"))) {

        msg_html[currentID]        += messageTemplate(val.photo, val.from_name, val.message);

        } else{

        msg_html[currentID]        += messageTemplate(val.photo, val.from_name, val.message);

        temp += currentID + ",";
        }

        });


some code ------------------

You can then display your html by using:

$.each(msg_html,function(index,val){
     $('#someID_'+index).html(val);
            ----
            ---
            ---
});
Sign up to request clarification or add additional context in comments.

2 Comments

but for var msg_html=array(); ReferenceError: array is not defined
Fixed the answer to implement your corrections. If the answer fits your needs, please check the question as answered.
0

You can use eval.

E.g.,

$.each(_d, function(index, val) {

    var currentID=val.id;

    if(temp.match(new RegExp("(?:^|,)"+currentID+"(?:,|$)"))) {

        eval("msg_html"+'_'+currentID +" += " + messageTemplate(val.photo, val.from_name, val.message));

    } else{

        eval("msg_html"+'_'+currentID +" += " + messageTemplate(val.photo, val.from_name, val.message));

    temp += currentID + ",";
    }

});

1 Comment

Wouldn't you need to define the variables before the loop, and assign them to empty strings? (Otherwise the variable names being evaled don't exist so trying to use += will give an error.)
0

I'm not sure this is what you want but you can try to use use StartsWith selector along with each():

$("[id^='someID_']").each(function(index) {
    $(this).html(msg_html + '_' + index);
});

Fiddle

Comments

0

Use a object

var temp = ""; var msg_html = {};

$.each(_d, function(index, val) {
    var currentID = val.id;
    msg_html['_' + currentID] = '';
    if (temp.match(new RegExp("(?:^|,)" + currentID + "(?:,|$)"))) {
        msg_html['_' + currentID] += messageTemplate(val.photo, val.from_name, val.message);
    } else {
        msg_html['_' + currentID] += messageTemplate(val.photo, val.from_name, val.message);
        temp += currentID + ",";
    }
});

Then

$('#someID_0').html(msg_html._0);
$('#someID_1').html(msg_html._1);
$('#someID_2').html(msg_html._2);

4 Comments

I need assignment "+=" not "="
@JomyJoseph why do you want +=
i need total data , not last one

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.