Leonard Febrianto Leonard Febrianto - 4 months ago 10
CSS Question

Can't change font color onClick inside PHP & Javascript

I just want to change font color in href with function. But i seems troubling with quot.

This is my code :

<script>
<?php foreach($locations AS $loc) { //you could replace this with your while loop query ?>
var x = '<?php echo $loc['tgl'];?>';
addMarker(<?php echo $loc["lat"]; ?>, <?php echo $loc["ltd"]; ?> ,'<?php echo $loc["order"]; ?>',x );
document.getElementById('left').innerHTML += "<?php echo "<li><a href='#' id='".$loc['order']."' onClick='changeColor('".$loc['order']."');return false;'>".$loc["order"]."</a></li>";?>";
<?php } ?>


And this is the function :

function changeColor(id)
{
document.getElementById(id).style.color = "#1FFF48"; // forecolor
}
</script


I have tried just only inside in HTML and it succeed. But inside javascript im pretty confused. Any help ?

Answer

Use like this, it will work

<?php $order="onClick=changeColor('".$loc['order']."');return false;";  ?>
    document.getElementById('left').innerHTML += "<?php echo "<li><a href='#' id='".$loc['order']."' $order >".$loc["order"]."</a></li>";?>";