1

I have a jQuery statement

 $('.searchDisplay').append('<i class="ion-email" onclick="copyEmail('+data[i].email+')"></i>' );

Here if I click this icon then this function copyEmail() is called which takes a string as an argument and adds it to the clipboard.

But when I click on the icon I get an error in my console

Uncaught SyntaxError: Invalid or unexpected token

then I tried doing it this way

$('.searchDisplay').append('<i class="ion-email" onclick="copyEmail("'+data[i].email+'")"></i>' );

I got this error

Uncaught SyntaxError: Unexpected token }

The problem is that I'm not able to pass a string in function.

Can someone slove this for me ?

1
  • I don't believe your problem is in the posted code, please edit question and post the function 'copyEmail', and check the reported line number from the console where the SyntaxError comes up Commented Mar 4, 2018 at 9:51

6 Answers 6

1
 $('.searchDisplay').append('<i class="ion-email" onclick="copyEmail(\''+data[i].email+'\')"></i>' );

Problem in your code was...

When you call a function with string as an argument you do someFunction('somestring');, in your code you are missing the '' wrapping the string.

So you need to wrap data[i].email with ' .. '. I have wrapped the data[i].email with '' but as there was already one pair so I had to escape it with \.

Sign up to request clarification or add additional context in comments.

1 Comment

Can you explain to me what was the problem?
1

Try this

$('.searchDisplay').append('<i class=\"ion-email\" onclick=\"copyEmail(\"'+data[i].email+'\")\"></i>' );

Comments

1

You should use "\" to avoid the errors, so you can make this:

$('.searchDisplay').append('<i class="ion-email"  onclick="copyEmail(\''+data[i].email+'\')"></i>' )

Edited

Comments

1

You do not need to wrap data[i].email with '+ +' at all in your function argument:

$('.searchDisplay').append('<i class="ion-email" onclick="copyEmail(data[i].email)"></i>' );

var data = [{'email': '[email protected]'}, {'email': '[email protected]'}];
var i = 1;
$('.searchDisplay').append('<i class="ion-email" onclick="copyEmail(data[i].email)">Click To Test</i>' );

function copyEmail(email){
  console.log(email);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="searchDisplay"></div>

Comments

0

You're better to give it an id, or another identifying attribute, and then add the onClick handler after appending it, when it's available in the DOM, along the lines of:

$('.searchDisplay').append('<i id="num' + i + '" class...></i>');
$('#num' + i).on('click', 
                 function () {
                     ...
                 });

Or hook into all of them at once by selecting the .searchDisplay class. You can use $(this) in the function to determine which one was clicked.

Comments

0
$('.searchDisplay').append(`<i class="ion-email" onclick="copyEmail('${data[i].email}')"></i>`);

1 Comment

While this code may answer the question, providing additional context regarding how and why it solves the problem would improve the answer's long-term value.

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.