Reputation: 624
I want to store a javascript onclick function code into a string first and then i need to pass it in a radio button.
My sting variable is:
$jsFunc = "onclick=\"javascript:showHide(this.value, \'$arrFlipEntities[$field_id]\');\"";
I want to use this variable like dis:
$strCode .= "<input {$jsFunc} type=\"radio\" name=\"{$field_name}\" id=\"{$option}\" value=\"{$option}\" checked=\"checked\"/><label for=\"{$option}\">{$option}</label>";
But it is giving this output, when i viewed the source code:
<input onclick="javascript:showHide(this.value, \'bas_link\');" type="radio" name="bas_type" id="Yes" value="Yes" checked="checked">
Upvotes: 1
Views: 65
Reputation: 5479
I prefer the single quote method like so:
$jsFunc = 'onClick="javascript:showHide(this.value, '.$arrFlipEntities[$field_id].');"';
$strCode .= '<input '.$jsFunc.' type="radio" name="'.$field_name.'" id="'.$option.'" value="'.$option.'" checked="checked"/><label for="'.$option.'">'.$option.'</label>';
Upvotes: 1
Reputation: 214
when ' is in two ",it doesn't need \
$jsFunc = "onclick=\"javascript:showHide(this.value, '$arrFlipEntities[$field_id]')\"";
$strCode .= "<input {$jsFunc} type=\"radio\" name=\"{$field_name}\" id=\"{$option}\" value=\"{$option}\" checked=\"checked\"/><label for=\"{$option}\">{$option}</label>";
Upvotes: 1
Reputation: 100175
Try:
$jsFunc = "onclick=\"javascript:showHide(this.value, '".$arrFlipEntities[$field_id]."');\"";
$strCode .= "<input {$jsFunc} type=\"radio\" name=\"{$field_name}\" id=\"{$option}\" value=\"{$option}\" checked=\"checked\"/><label for=\"{$option}\">{$option}</label>";
Upvotes: 1
Reputation: 1344
$jsFunc = "onclick=\"javascript:showHide(this.value, \'$arrFlipEntities[$field_id]\');\"";
Should be:
$jsFunc = "onclick=\"javascript:showHide(this.value, '".$arrFlipEntities[$field_id]."');\"";
Single and double quotes don't get escaped when they are wrapped by the other. Also you can't access an array like that inside double quotes, only works for variables like $var = "$myVar";
Upvotes: 0