KaoriYui
KaoriYui

Reputation: 922

Javascript onClick function SyntaxError: expected expression, got '}'

I am trying to pass a parameter from a onClick event using javascript but got a undefined value error, after reading from the questions here at SO and as suggested you need to add and escape the parameter by using \'\ but when applying the solution i am getting the SyntaxError: expected expression, got '}' on execution, Any suggestions would be great!

Here is my actual code.

rootRef.on("child_added", snap => {

   var valkey = snap.key;
   var name = snap.child("Name").val();
   var email = snap.child("Email").val();

   $('#table_body').append('<tr><td>'+ name +'</td><td>'+ email +'</td><td><a onClick="deleteMe(' + "'" + valkey + "'" + ')">delete</a></td></tr>');

});

function deleteMe(valkey){
    var cKey = valkey;
    console.log(cKey);

}

Here is a code snippet similar to my problem.

$(document).ready(function(){

var valkey = "hello!";

$('#table_body').append("<a onClick='deleteMe(\'" + valkey + "\')'>delete</a>");


function deleteMe(valkey){
    
    var cKey = valkey;
    
    console.log(cKey);

}


});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="table_body">

</div>

Upvotes: 0

Views: 1462

Answers (2)

JG73
JG73

Reputation: 100

this work for me. remove \' and '

replace this <a onClick='deleteMe(\'" + valkey + "\')'>
for this     <a onClick=deleteMe('" + valkey + "')>

Paste this in console and try, work for me

var email ="email";
var valkey ="valueyodelete"; 
$('#table_body').append("<tr><td>"+ name +"</td><td>"+ email +"</td><td><a onClick=deleteMe('" + valkey + "')>delete</a></td></tr>");


function deleteMe(valkey){
    var cKey = valkey;
    console.log(cKey);

}

Upvotes: 1

Kosh
Kosh

Reputation: 18413

Try this:

$('#table_body').append('<tr><td>'+ name +'</td><td>'+ email +'</td><td><a onClick="deleteMe(' + "'" + valkey + "'" + ')">delete</a></td></tr>');

Upvotes: 2

Related Questions