Claude Grecea
Claude Grecea

Reputation: 543

Uncaught SyntaxError: Unexpected token Error

I am have a problem with the following Uncaught SyntaxError: Unexpected token Error after switching from an input box to textarea. This error occurs when trying to delete the text entered into the textarea after submitting it.

textarea:

<textarea name="chatter"></textarea>

Function call:

<a href='javascript:void(0);' 
 onClick='deletecmnt(this, '".$val['id']."', '".BASE_URL."');' 
  title='Delete Chatter'>X</a>; 

delete function:

function deletecmnt(obj, cmt_id, baseurl){
    var lg_chk = loginchk();
    if(lg_chk){
        var object = $(obj).parent().parent();
        $.ajax({
            type: "GET",
           // error: function(error){console.log("Error:");console.log(error);},
            url:baseurl+'deletechatter.php?id='+cmt_id,
            dataType:'json',
            success:function(response){
                object.remove();
            }
        });
    }
    else
        location.href=baseurl;
}

Ok so inside Chrome I receive the error stated above but in Firefox I receive the following:

 SyntaxError: syntax error      

 deletecmnt(this, 

However, I don't see anything wrong with it and it worked when I was using a input box. I even switched it back and it worked so what is it about the textarea that it does like? Please let me know what I am doing wrong.

Here is what I got so far:

<a data-id='".$val['id']."' data-base-url='".BASE_URL."' href='javascript:void(0);' title='Delete Chatter'>x</a>

<script>$("a[title='Delete Chatter']").on('click', deletecmnt);</script>

function deletecmnt(obj, cmt_id, baseurl){
var lg_chk = loginchk();
if(lg_chk){
    var object = $(this).parent().parent();
    $.ajax({
        type: "GET",
       // error: function(error){console.log("Error:");console.log(error);},
        url:baseurl+'deletechatter.php?id='+cmt_id,
        dataType:'json',
        success:function(response){
            object.remove();
        }
    });
}
else
    location.href=baseurl;

}

Upvotes: 1

Views: 6299

Answers (2)

Explosion Pills
Explosion Pills

Reputation: 191819

You are closing the apostrophe prematurely:

deletecmnt(this,

...becomes the entire onclick. Since you are using jQuery, it makes a lot more sense to bind with it (even if you weren't, I would still suggest doing so with JS).

$("a[title='Delete Chatter']").on('click', deletecmnt);

You can update deletecmnt so that it references this, which would be the anchor. Also, update the anchor to store the ID and BASE_URL as part of the DOM, possibly with:

<a data-id='".$val['id']."' data-base-url='".BASE_URL."'

Upvotes: 1

sachleen
sachleen

Reputation: 31141

The quotes are wrong:

onClick='deletecmnt(this, "'.$val['id'].'", "'.BASE_URL.'");' 

Look how you open and close single quotes.

Upvotes: 1

Related Questions