3

How can I add a param to the removeSound function in this JavaScript code?

I cannot get it to work.

var param = "111";

coder = '<a href="javascript:;" onClick="javascript: removeSound(' + param + ');" >Delete</a>'

Update

I do not know why code = was removed! This is needed to explain the problem context.

4
  • I just need to know how to add a param to the function without breaking it. The function it self does not matter. Commented Jan 19, 2012 at 10:36
  • If it doesn't work with single quotes then your function is erroneous. Commented Jan 19, 2012 at 10:39
  • I have added that extra bit of information to my answer. Commented Jan 19, 2012 at 11:24
  • ah, are you trying to put a piece of html code into a variable called code/coder? Commented Jan 19, 2012 at 11:32

4 Answers 4

3

I just want to add a proper double quoting escape method to the answers as none showed a correct way to escape double quotes inside of an onClick attribute.

In HTML it is not sufficient to use only a backslash when escaping a JavaScript double quote. You need the proper HTML entity which is &quot;

This is a live example:

<a href="#" onClick="removeSound(&quot;121212&quot;);">Delete</a>

However for readability I would recommend using Antony Scott's way by using single quotes.

<a href="#" onClick="removeSound('121212');">Delete</a>

To add the param as a variable from whatever script your HTML is generated in you should do this:

code = '<a href="javascript:;" onClick="javascript: removeSound(&quot;' + the_param + '&quot;);" >Delete</a>';

The other possible way should be:

code = '<a href="javascript:;" onClick="javascript: removeSound(\'' + the_param + '\');" >Delete</a>';
Sign up to request clarification or add additional context in comments.

2 Comments

Works fine! How can I add two params?
This really depends on the type. If it is of type String then you'd have to add a coma after the escaped single quote. If it is of any other type then you don't quote them at all and just separate them with a coma.
3

You need to use different quotes. Try something like this ...

<a href="javascript:;" onClick="javascript: removeSound('PARAM HERE');" >Delete</a>

3 Comments

I have updated my question and example to better reflect what I needed to do.
did you want the 111 to be a string or a number within your removeSound function?
@JonathanClark Indeed, you gotta pay attention to that since all the methods we have shown you will have the parameter be a string.
0

try this

code = '<a href="#" onclick="removeSound("PARAM HERE");">Delete</a>';

1 Comment

I have updated my question and example to better reflect what I needed to do.
0

try this

 <a href="#" onClick="removeSound('121212')" >Delete</a>

1 Comment

I have updated my question and example to better reflect what I needed to do.

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.