0

I need to call a function with parameters in a jQuery append method and want to pass a variable named image but I keep getting syntax error even when I pass a static value

$("#officialDiv").append("<a onclick='remove('" + image + "');' class='btn btn-danger btn-sm'></a>");

or static

$("#officialDiv").append("<a onclick='remove('test');' class='btn btn-danger btn-sm'></a>");

function remove(file) {
}

below a screenshot of the error

enter image description here

1
  • What is image? Commented Mar 8, 2018 at 10:48

6 Answers 6

4

That is because your HTML string is breaking on '. You need to escape the quotes.

$("#officialDiv").append("<a onclick=\"remove(\'" + image + "\');\" class='btn btn-danger btn-sm'>Link</a>");

Should work fine.

Demo

var image = "some_data";
$("#officialDiv").append("<a onclick=\"remove(\'" + image + "\');\" class='btn btn-danger btn-sm'>Link</a>");

function remove(a){
  console.log(a+" is removed");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="officialDiv"></div>

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

3 Comments

i already tried it and it did not work i keep getting this error JavaScript critical error at line 1, column 7 in (unknown source location)\n\nSCRIPT1002: Syntax error
@User7291 you sure this is the line causing this error?
@User7291 there was minor issue. See the updated answer.
1

It appears the issue is probably within the image variable.

That said, we can use template literals to reduce the quotes confusion.

$("#officialDiv").append(`<a onclick="remove(${image});" class="btn btn-danger btn-sm">Image</a>`);

Here's a working example:

var image = "myImage";

$("#officialDiv").append(`<a onclick="remove('${image}');" class="btn btn-danger btn-sm">Image</a>`);

const remove = function(name) {
  alert(name);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id="officialDiv"></div>

2 Comments

it did not work i keep getting Invalid character error
Yes, I believe the issue is in the image variable. You'll need to share the image variable with us.
1

In my case, all the above solutions were not accomplished my requirement.

$('.my-list').append('<a href="javascript:openFile(\''+ dataId +'\')"><li class="list-group-item">'+ itemName +'</li></a>');

Comments

0

var test = 'test';
		$("#officialDiv").append("<a onclick='remove(test)' class='btn btn-danger btn-sm'>click me</a>");

		function remove(file) {
			alert(file);
		}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div id='officialDiv'></div>

Comments

0

How about this approach?

var anchor = $("<a class='btn btn-danger btn-sm'>click me</a>");
anchor.on('click', function () {
    remove(image)
});

$("#officialDiv").append(anchor);

function remove(file) {
    alert(file)
}

Comments

0

You could untie the behavior through an event handler thus removing the issue (additional benefit you get is cleaner, reusable code):

<a onclick="onRemoveClick()" ... >

function remove(value) { ... } 
function onRemoveClick() { remove('image'); }

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.