JQUERY object with dynamic string

Asked

Viewed 23 times

0

I have a for() that adds some elements in the div#chat_box according to the JSON returned in an AJAX. Only that I would like to see how many data-Identity elements equal to those returned in JSON exist. So I did so:

// AJAX -> SUCCESS: FUNCTION(data){
// data É RETORNADO EM JSON
for( var i = 0; i < data.msgs; i++){
										
							if($('.msg[data-identity=' + data[i]['identity'] + ']').lenght){
											$('#chat_box').append('<div data-identity="' + data[i]['identity'] + '" class="msg user_' + data[i]['time'] + '"><span>'+ data[i]['nick'] + ': </span>' + data[i]['contente'] +'</div)');	
							}		
}
//}

Only it doesn’t work and I think it’s because of that line:

if($('.msg[data-identity=' + data[i]['identity'] + ']').lenght){

But I don’t know how to put a dynamic parameter (date[i]['Identity']) in the jquery object ($('.msg..']), it doesn’t work the way I put it).

Can anyone help?

  • Couldn’t quite understand your doubt, you need to check if the $('.msg[data-Identity=' + date[i]['Identity'] + ']') element is the same as in json ? or if you have the same amount of that element as what you have within json ? tries to describe a little better what the problem is

  • Basically I want to know if I can use the jquery object this way $('.msg[data-Identity=' + data[i]['Identity'] + ']') or I can’t put a variable in there

  • 3

    The syntax is wrong. The correct would be: $('.msg[data-identity="' + data[i]['identity'] + '"]').length

  • Yes, even, depending on how your json is this code is right, so I asked to put a piece of json in the question, but it must be the error in the syntax, ps: the dvd comment should solve)

  • dvd posted as response :)

  • thank you all.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.