Reputation: 5298
Im trying to use inner html and dynamically set an onclick statement that contains a variable:
cell5.innerHTML="<input type='button' value='Remove' onclick='removerow('manual_" + (num) + "')' />"
So lets say the num variable was set to 4. The output should be onclick="removerow('manual_4')"
Any help?
Upvotes: 1
Views: 154
Reputation: 817128
The best thing would be to avoid all this:
var input = document.createElement('input');
input.type = 'button';
input.value = 'Remove';
input.onclick = function() {
removerow('manual_' + num);
};
cell5.appendChild(input);
Depending on the relationship between the button, the cell and the row to be removed, you could also do DOM traversal inside the handler to get a reference to the row.
Upvotes: 4
Reputation: 4173
Consider using a click event instead. It makes that sort of thing clearer:
$(".cell5") // whatever selector you want
.html('<input type="button" value="Remove"/>')
.click(function(){
removerow('manual_' + row);
});
Also consider using the row number as the input instead of a string. If they all have the same form you can simplify things with just:
removerow(row);
Clearly that is an API design decision, but it would simplify this expression if it makes sense.
Upvotes: 0
Reputation: 24370
cell5.innerHTML='<input type="button" value="Remove" onclick="removerow(\'manual_' + (num) + '\')" />'
Upvotes: 1
Reputation: 34863
You can escape the quotes using \
So
onclick=\"removerow('manual_" + (num) + "')\"
Upvotes: 0
Reputation: 38284
You can escaped nested quotes using \
:
"...onclick=\"removerow('manual_" + (num) + "')\"..."
Upvotes: 2