0

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>

0

2 Answers 2

2

Try this:

$('#table_body').append('<tr><td>'+ name +'</td><td>'+ email +'</td><td><a onClick="deleteMe(' + "'" + valkey + "'" + ')">delete</a></td></tr>');
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! than worked! Sorry for changing the question to add a code snippet similar to my problem.
1

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);

}

Comments

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.