JSFiddle - React, Tailwind, and code Playground
HTML
<script type="text/javascript">
window.onload=function() { attachBehaviors(); }
function attachBehaviors() {
// only doing one here, but you can add others
if (
document.getElementById('toggle_chk') &&
document.getElementById('my_img')
) {
// So JS is "aware" of the element style
document.getElementById('my_img').style.display='block';
// Attach the behavior
document.getElementById('toggle_chk').onclick=function() {
toggle_image(this);
};
}
}
//
function toggle_image(chk) {
// already checked for my_img
// if you want to hide it . . .
//document.getElementById('my_img').style.display=(chk.checked)?'block':'none';
// if you just want to swap src
var src=(chk.checked)?'http://i.imgur.com/Ms79t.jpg':'http://i.imgur.com/PHSf5.jpg';
document.getElementById('my_img').src=src;
}
</script>
</head>
<body>
<form action="">
<p><input type="checkbox" name="toggle_chk" id="toggle_chk" value="1" checked>
<label for="toggle_chk">Mudar imagem</label></p>
<p><img src="http://i.imgur.com/Ms79t.jpg" id="my_img"></p>
</form>
</body>