0

so i have this button, which is added dynamically in java script, but the thing is a want to send the files name as a parameter to the function, i already have the name in a var saved as file, the name is generally something like p164.epub. but for some reason it wont accept the parameter. if for instance i add a number instead of file, it accepts it and it works as it should

$('#result2').append("<button class ='removeBtn' onclick ='removeEbook("+file+")'>Remove</button>");

my function

function removeEbook(n)
{
  alert("yay");
}

any thoughts?

1
  • maybe a typo but double quotes are needed around "<button ... </button>" Commented May 22, 2013 at 15:36

3 Answers 3

3

You have jQuery - don't use inline event handlers:

var file = '...';

$('<button class="removeBtn">').text('Remove').on('click', function() {
    // NB: "this" refers to the clicked button
    removeEbook(file);
}).appendTo('$#result2');

If you insist on using inline event handlers (and you really shouldn't), best practise is to use single quotes for your JS strings, and double quotes for the attributes within your HTML, e.g.:

$('#result2').append('<button class="removeBtn" onclick="removeEbook(\''+file+'\')">Remove</button>');

Note that as written this will bind the handler to whatever value file has at the time the handler is registered (because the code above turns it into a string constant) instead of whatever value it has when it's triggered.

Note that if file happens to contain any quote characters (which is perfectly legal on some systems) you will end up with an illegal string constant.

The above are just two of the many reasons why inline event handlers shouldn't be used. They're error prone (as you've found).

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

1 Comment

gah - just had to correct an error in the escaped string version that got through because inline handlers and escaped strings are a right pain and shouldn't be used!
1

You need to add escaped quotes around the string parameter.

$('#result2').append("<button class ='removeBtn' onclick ='removeEbook(\'"+file+"\')'>Remove</button>");

1 Comment

+1 for escaping the right characters, instead of using the same quote marks everywhere and then having to quote too many!
-1

you have to send file name in single quotes and use escape character for double qoutes like belwo

$('#result2').append("<button class =\"removeBtn\" onclick =\"removeEbook('"+file+"')\">Remove</button>");

6 Comments

dude!!!! love you man, you dont understand how long iv been struggling with this!!! you sir are a hero
no, you don't have to do this, so long as you use single quotes for your JS strings, and double quotes for HTML attributes. If you must use inline event handlers (and you really shouldn't) then ogc-nick's answer is better than this, because it only escapes the innermost string, not every quote mark
@Alnitak, this is one of the solution. there can be different solutions for single problem. I have just corrected the used code to make it working.
wait, this turns it into a object at the function and it seems to be undefined, how would i go around accessing the element?
@user1752451 you can access the element by using a proper event handler!
|

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.