Hello -
How does one add a string variable argument to an event handler that is defined inside a string (typically used for setting innerHTML). Here's an example of what I'm trying to do: ( put idStr as a string arg to the onclick handler )
/****************************************/
function addCommentBox( idStr ) {
var el = document.createElement( "div" );
var input = "<INPUT TYPE='BUTTON' onclick='clk( this, "idStr" )' value='Ok'/>";
el.innerHTML = input;
document.body.appendChild( el );
}
Is there some solution that involves escaping quotes? Or maybe doing an eval?
Here's a WORKAROUND I developed - create an artificial attribute (tid) to which you set the idStr val, then access via this.tid in the event handler code. But it would be great to find a way to avoid this workaround!
WORKAROUND :
/****************************************/
function addCommentBox( idStr ) {
var el = document.createElement( "div" );
var input = "<INPUT TYPE='BUTTON' idStringVar='" +idStr +"'" +"onclick='clk( this )' value='Ok'/>";
el.innerHTML = input;
document.body.appendChild( el );
}
/****************************************/
function clk( obj ) {
alert("got2 testClick, +"id string var : " +obj.idStringVar);
event.cancelBubble = true;
}
Thx for your thoughts or suggestions! - Delvis
How does one add a string variable argument to an event handler that is defined inside a string (typically used for setting innerHTML). Here's an example of what I'm trying to do: ( put idStr as a string arg to the onclick handler )
/****************************************/
function addCommentBox( idStr ) {
var el = document.createElement( "div" );
var input = "<INPUT TYPE='BUTTON' onclick='clk( this, "idStr" )' value='Ok'/>";
el.innerHTML = input;
document.body.appendChild( el );
}
Is there some solution that involves escaping quotes? Or maybe doing an eval?
Here's a WORKAROUND I developed - create an artificial attribute (tid) to which you set the idStr val, then access via this.tid in the event handler code. But it would be great to find a way to avoid this workaround!
WORKAROUND :
/****************************************/
function addCommentBox( idStr ) {
var el = document.createElement( "div" );
var input = "<INPUT TYPE='BUTTON' idStringVar='" +idStr +"'" +"onclick='clk( this )' value='Ok'/>";
el.innerHTML = input;
document.body.appendChild( el );
}
/****************************************/
function clk( obj ) {
alert("got2 testClick, +"id string var : " +obj.idStringVar);
event.cancelBubble = true;
}
Thx for your thoughts or suggestions! - Delvis