Hello, I have problem with my JS function.
Please help me, I need this code until today and I can’t understand what is the wrong 🙁
foreach($group as $i => $name){
print'<li onclick="groups(\'group'.$i.'\');" >'.$name.'</li>';
}
Then I have this code:
$moretags = "<script>";
$moretags .= 'function groups(grid){';
foreach($group as $i => $name){
$moretags .= '
if(grid == \'group'.$i.'\'){
document.getElementById(\'show\').innerHTML = \'group'.$i.'\';
}
';
}
$moretags .= '</script>';
I completely agree with Felix Kling that you shouldn’t generate JavaScript code with PHP code unless you really know what you’re doing, which you apparently do not.
It is impossible to solve your problem because of the lack of details in your question but I’ll try to at least help you make your code less error prone.
All you should generate in your PHP should be the HTML tags (at most!) without any
onclickattributes, onlyclasses and maybe someids. Then just add the relevant event handlers in JavaScript (and use some framework like jQuery – read this to see why) or you can even add the HTML elements in JavaScript if they can’t be used without JavaScript anyway.Code generation is a hard thing. Generating JavaScript in PHP won’t get you very far.