0

I was going through the solutions of Uncaught SyntaxError: missing ) after argument list, and tried every other way to sort it out but I am still getting this error with this code

$("input").on("keypress",function(event){

if(event.which === 13)
{
    var ToDotext=($(this).val());   

$("ul").append("<li><span><i class="fa fa-trash"></span> "  +  ToDotext  + "</li>");
$(this).val("");
}

} );

whenever I put <i class="fa fa-trash" in <span></span> I am getting this error, without <i class="fa fa-trash"> things are working fine.

shaykoo
  • 61
  • 1
  • 13

4 Answers4

2

Change the appending line to this.

$("ul").append("<li><span><i class='fa fa-trash'></i></span> "  +  ToDotext  + "</li>");

Changes:

  1. fa fa-trash is in single quotes as you are using double quotes outside.
  2. close the i tag

Suggestions:

  1. Instead of using <i> inside <span>, apply the class on span or remove span altogether and keep i tag.

  2. Use jQuery element creation methods. like this.

    $('ul li ').html($('<i>', {class: 'fa fa-trash'}));

Community
  • 1
  • 1
Prajwal
  • 3,930
  • 5
  • 24
  • 50
1

Use this code

   $("input").on("keypress",function(event){
    if(event.which === 13)
       {
           var ToDotext=($(this).val());   

        $("ul").append("<li><span><i class='fa fa-trash'></i></span>"+ ToDotext  + "</li>");

         $(this).val("");
     }
    });
Negi Rox
  • 3,828
  • 1
  • 11
  • 18
1

I found the error in your script.

$("ul").append("<li><span><i class="fa fa-trash"></span>"+ToDotext+"</li>");

If you add component, with this code, the actual components are added as follows.

<li><span><i class=fa fa-trash></span>ToDoText</li>

As you see above, the class names of the <i> tag must be quoted with " or ' but it is not like that. It is because you used the same quotes adding the components. If you change "fa fa-trash" -> 'fa fa-trash', the problem will be solved.

Pang
  • 9,564
  • 146
  • 81
  • 122
0

You can't have double quotations inside double quotations, simple example:

open your console:

var newElem = "<h1 class="rock">Hello There !!</h1>";
newElem // Uncaught SyntaxError: Unexpected identifier

You will have to enclose you class and other html attributes inside a single quotations, like so:

var newElem = "<h1 class='rock'>Hello There !!</h1>";  

The below line:

$("ul").append("<li><span><i class='fa fa-trash'></span> "  +  ToDotext  + "</li>");

Would have to change to:

$("ul").append("<li><span><i class="fa fa-trash"></span> "  +  ToDotext  + "</li>");

Additional ::- if you writing long concatenations you probably want to SPLIT IT UP INTO ADDITIONAL LINK

Alexander Solonik
  • 9,838
  • 18
  • 76
  • 174