5

My code is as follows:

        <script>
        var i = 2;
        $("document").ready(function(){
            $("#newrow").click(function(){
                $("#maintable").append('<tr>
                                        <td><input type="text" name="dept_" + i size="5" maxlength="5" /></td>
                                        <td><input type="text" name="hours_" + i size="5" maxlength="1" /></td>
                                    </tr>');
            });
            i = i + 1;
        });
    </script>

Whenever I run it, JavaScript gives me an "Uncaught SyntaxError: Unexpected Token ILLEGAL" on the $("#maintable").append line.

For the life of me, I can't figure out what the syntax error is.

It's not a problem with the actual element being appended, because I tried just '<td></td>' as well and that got the same error.

2 Answers 2

13

You can't break a string over multiples lines without special treatment. Either put it all on one line, or escape the newlines with backslashes:

'<tr>\
     <td><input type="text" name="dept_"' + i + ' size="5" maxlength="5" /></td>\
     <td><input type="text" name="hours_"' + i + ' size="5" maxlength="1" /></td>\
</tr>'
Sign up to request clarification or add additional context in comments.

Comments

2
<script>
    var i = 2;
    $(document).ready(function(){
        $("#newrow").click(function(){
            var html = '<tr>';
                html += '<td><input type="text" name="dept_"'+i+' size="5" maxlength="5" /></td>';
                html += '<td><input type="text" name="hours_"'+i+' size="5" maxlength="1" /></td>';
                html += '</tr>'
            $("#maintable").append(html);
        });
        i++;
    });
</script>

document does not have quotes, right now your "ready" function does not work! The variable "i" is not added at all! A string can't be divided over multiple lines unless you use newlines, add to a variable, or something similar.

6 Comments

It's working fine with the quotes still there. I actually realized after downvoting that that was a little harsh, but StackOverflow won't let me undo it. Edit: Figured out how.
Actually, it will work, so my answer is somewhat wrong, but there is no reason for the quotes when referencing the document or window, and in my opinion you should not have the quotes there!
What version of jQuery lets you select with $("document")?
@Dennis - It will actually work, FIDDLE, but it just does'nt seem like a good idea, as that would make it a string, and not the native JS document variable, or atleast so I think?
To quote the documentation: The .ready() method can only be called on a jQuery object matching the current document, so the selector can be omitted. "document" will not actually select anything but apparently that doesn't matter.
|

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.