0

I am trying to add a button from a javascript function (with the innerHTML method) that has another function as the onClick function. This onClick function has to take multiple parameters:

area.innerHTML += "<input type='image' src='images/del.jpg' width='15' height='15' " +
           "onClick='del(" + info['_id'] + "," + key + "," + value + ")' />";

This is (I think) the most logical way to do this, but when I try to click the button there is no functioncall. This works with one single argument, so the problem has to be in the combination of the parameters with the ",". Does anybody know how to solve this?

update The del function is a temporary testfunction

function del(id, key, value) {
    console.log(id + key + value);
}
5
  • It looks to me like it should work. Are you able to post the code to your del function? Commented Apr 13, 2012 at 23:54
  • 1
    Are there any errors? What types of data are info[_id], key and value? Commented Apr 14, 2012 at 0:04
  • What is _id, just string or variable ?? Commented Apr 14, 2012 at 0:05
  • <button><img src="del.jpg"></button> is better than <input type="image"> (but needs style hacks if you're forced to cater to IE6). Attaching event handlers from external JS is better than using inline onclick handlers. Inspecting the attributes and the DOM to find your id, key, and value is better than string concatenation. In summary, using your (JS) controller to manipulate your view (the DOM) is better than mixing the two together. IMHO. (my humble opinion is based on 20 years of event-driven programming. take it as you will.) Commented Apr 14, 2012 at 0:12
  • info[_id], key and value are normal strings and were tested upfront an d there aren't any errors Commented Apr 14, 2012 at 0:30

1 Answer 1

3

I think you just need your variabled to be passed as strings. Edit the onclick even to this:

onClick='del(\"" + info['_id'] + "\",\"" + key + "\",\"" + value + "\")' />"
Sign up to request clarification or add additional context in comments.

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.