0

I'm trying to figure out how I can do an if statement with this code. The if statement is supposed to determine whether or not the li gets a class name or not depending on if its read or not. What determines it is the data[x][messageRead] == 0 or not but not sure how to do this inside of this append.

$.each(data, function(x)
{
    $('.mail').append('<li><a href=""><strong>Received: '+data[x]['dateSent']+'</strong><small>'+data[x]['subject']+'</small><small>From: '+data[x]['firstName']+' '+data[x]['lastName']+'</small></a></li>'); 
});
1
  • Lots of good solutions here... Commented Mar 20, 2012 at 15:54

9 Answers 9

2

You could use the ternary operator ?:

$.each(data, function(x)
{
    $('.mail').append('<li' + (data[x][messageRead] == 0 ? 'class="ClassName"' : '') + '><a href=""><strong>Received: '+data[x]['dateSent']+'</strong><small>'+data[x]['subject']+'</small><small>From: '+data[x]['firstName']+' '+data[x]['lastName']+'</small></a></li>'); 
});
Sign up to request clarification or add additional context in comments.

Comments

1

Just javascript...

$.each(data, function(x)
{
    if (data[x][messageRead] == 0) {
        $('.mail').append('<li><a href=""><strong>Received: '+data[x]['dateSent']+'</strong><small>'+data[x]['subject']+'</small><small>From: '+data[x]['firstName']+' '+data[x]['lastName']+'</small></a></li>');
    } else {
        $('.mail').append('<li class="read"><a href=""><strong>Received: '+data[x]['dateSent']+'</strong><small>'+data[x]['subject']+'</small><small>From: '+data[x]['firstName']+' '+data[x]['lastName']+'</small></a></li>');
    }
});

You can refactor a bit but principle is like anything else

Comments

1

For such cases it is advised to use the method .toggleClass() from the jQuery API

http://api.jquery.com/toggleClass/

Comments

1

The actual value gets passed to the second value of your callback. Then you create the li element first and then use a strict equality comparison if you should add the class:

$.each(data, function(x, value)
{
    var li = $('<li><a href=""><strong>Received: '+ value.dateSent +'</strong><small>'+ value.subject +'</small><small>From: '+ value.firstName+' '+ value.lastName +'</small></a></li>');
    if(value.messageRead === 0) {
        li.addClass('the-class');
    }
    $('.mail').append(li);
});

Comments

1

Try the ternary operator

$.each(data, function(x)
{
    $('.mail').append('<li' + (data[x][messageRead] == 0 ? ' class="yourclass"' : '')><a href=""><strong>Received: '+data[x]['dateSent']+'</strong><small>'+data[x]['subject']+'</small><small>From: '+data[x]['firstName']+' '+data[x]['lastName']+'</small></a></li>'); 
});

Should do the trick.

Comments

1

You can do something like this:

$.each(data, function(x)
{
    $('.mail').append('<li' + (data[x][messageRead] == 0 ? ' class="myClass"' : '') + '><a href=""><strong>Received: '+data[x]['dateSent']+'</strong><small>'+data[x]['subject']+'</small><small>From: '+data[x]['firstName']+' '+data[x]['lastName']+'</small></a></li>'); 
});

Comments

1

Do something like this:

$.each(data, function(x)
{
    $('.mail').append('<li class="' + ((data[x][messageRead] == 0)?'read':'unread') + '"><a href=""><strong>Received: '+data[x]['dateSent']+'</strong><small>'+data[x]['subject']+'</small><small>From: '+data[x]['firstName']+' '+data[x]['lastName']+'</small></a></li>'); 
});

Hope it helps!

Comments

1

I believe this would give you what you want.

$.each(data, function(x)
{
    var item = $('<li><a href=""><strong>Received: '+data[x]['dateSent']+'</strong><small>'+data[x]['subject']+'</small><small>From: '+data[x]['firstName']+' '+data[x]['lastName']+'</small></a></li>');
    if(data[x]['messageRead'] == 0){
        item.addClass('new');
    }
    $('.mail').append(item); 
});

Comments

1

You can use the ternary operator, and add the class only if your criteria is met.

For readability I store the class in variable, but it could of course be done within the append as well.

$.each(data, function(x)
{
    var classString = (data[x]['messageRead'] == 0) ? " class=\"read\"" : "";
    $('.mail').append('<li' + classString +'><a href=""><strong>Received: '+data[x]['dateSent']+'</strong><small>'+data[x]['subject']+'</small><small>From: '+data[x]['firstName']+' '+data[x]['lastName']+'</small></a></li>'); 
});

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.