0

I have a problem. I want to create a string (variable) in JS, using conditions, as follows:

var seanceHtml = '<table>' +
    '<tr><td>{{ Lang::get("messages.Monday") }}</td><td></td></tr>' +
        $.each(seances, function (index, seance)
        {

            if(seance.day == 0)
            {
                if(typeof seance.start_date === 'undefined' && typeof seance.end_date === 'undefined')
                {
                    <td><a><span class="fa fa-plus"></span></a></td>
                }
            }
        }) +
    '<tr><td>{{ Lang::get("messages.Tuesday") }}</td><td></td></tr>' +
    '<tr><td>{{ Lang::get("messages.Wednesday") }}</td><td></td></tr>' +
    '<tr><td>{{ Lang::get("messages.Thursday") }}</td><td></td></tr>' +
    '<tr><td>{{ Lang::get("messages.Friday") }}</td><td></td></tr>' +
    '<tr><td>{{ Lang::get("messages.Saturday") }}</td><td></td></tr>' +
    '</table>';

It said I have a problem in the syntax.

2
  • 1
    Where does it say you have a problem with the syntax? What is the exact error? Commented Feb 25, 2016 at 22:03
  • $.each returns an object, not a string. Close your string, add to it in the middle of the if, and then start adding to the string after you're done. Or use $.map().join(). Commented Feb 25, 2016 at 22:46

1 Answer 1

2

You need to do the concatenation inside the jQuery.each() function like the following.

var seanceHtml = '<table>' +
         '<tr><td>{{ Lang::get("messages.Monday") }}</td><td></td></tr>';
$.each(seances, function (index, seance) {
    if(seance.day == 0) {
        if(typeof seance.start_date === 'undefined' && typeof seance.end_date === 'undefined') {
            seanceHtml += '<td><a><span class="fa fa-plus"></span></a></td>';
        }
    }
});
seanceHtml += '<tr><td>{{ Lang::get("messages.Tuesday") }}</td><td></td></tr>' +
    '<tr><td>{{ Lang::get("messages.Wednesday") }}</td><td></td></tr>' +
    '<tr><td>{{ Lang::get("messages.Thursday") }}</td><td></td></tr>' +
    '<tr><td>{{ Lang::get("messages.Friday") }}</td><td></td></tr>' +
    '<tr><td>{{ Lang::get("messages.Saturday") }}</td><td></td></tr>' +
    '</table>';

See this demo.

seances = [{start_date : 'asdasdasd', end_date : 'asdasdasd', day : 0 },{start_date : 'asdasdasd', end_date : 'asdasdasd'},{start_date : 'asdasdasd', end_date : 'asdasdasd'}];

seanceHtml = '<table>' +
         '<tr><td>{{ Lang::get("messages.Monday") }}</td><td></td></tr>';
$.each(seances, function (index, seance) {
    if(seance.day == 0) {
        if(typeof seance.start_date === 'undefined' && typeof seance.end_date === 'undefined') {
            seanceHtml += '<td><a><span class="fa fa-plus"></span></a></td>';
        }
    }
});
seanceHtml += '<tr><td>{{ Lang::get("messages.Tuesday") }}</td><td></td></tr>' +
    '<tr><td>{{ Lang::get("messages.Wednesday") }}</td><td></td></tr>' +
    '<tr><td>{{ Lang::get("messages.Thursday") }}</td><td></td></tr>' +
    '<tr><td>{{ Lang::get("messages.Friday") }}</td><td></td></tr>' +
    '<tr><td>{{ Lang::get("messages.Saturday") }}</td><td></td></tr>' +
    '</table>';


document.getElementById('text').innerHTML = seanceHtml;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<textarea id="text" rows="10" cols="80"></textarea>

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

7 Comments

Could you indicate where and what the error was so OP can learn from his mistake?
@MikeC I am adding the details.
the error is "Uncaught SyntaxError: Unexpected token }" is the line inside the second if statement.
@MoslemCherif that was because we missed the apostrophe for the text inside each loop.
Same problem always with this solution
|

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.