AMG AMG - 1 month ago 6
HTML Question

PHP JavaScript echo won't work

Hello i currently have this PHP/JS code:

echo "<script>
function transformCard() {
document.getElementById('cardImage').src = " . $row['transformedimageurl'] . "
document.getElementById('transformbtn').onClick = deformCard();
}
function deformCard() {
document.getElementById('cardImage').src = " . $row['imageurl'] . "
document.getElementById('transformbtn').onClick = transformCard()
}
</script>";


And these are my button and image:

<button id="transformbtn" onClick="transformCard()"></button>
<img id='cardImage' src="image.jpg" />


But it isn't changing my Image
SRC
value and the
onClick
value for my button won't change too i'm really frustrated right now

Does anybody knows what i do wrong?

Thanks in Advence

Answer

Put the src in quotes:

echo "<script>
    function transformCard() {
    document.getElementById('cardImage').src = '" . $row['transformedimageurl'] . "';
    document.getElementById('transformbtn').onClick = deformCard();
}
function deformCard() {
    document.getElementById('cardImage').src = '" . $row['imageurl'] . "';
    document.getElementById('transformbtn').onClick = transformCard()
}
</script>";

You also probably want to remove the () on the onClick handler:

document.getElementById('transformbtn').onClick = deformCard;
document.getElementById('transformbtn').onClick = transformCard;