What I am trying to do is load a page, get a PHP variable into JavaScript, and set my element (Link) to said variable. Then, when you click on the link, I would like it to change from open or close to close or open. For some reason, the other solutions I have looked at don't seem to work. What seems to be the issue with the following code? I'm slightly confused as to why this isn't working.
<script type="text/javascript">
var state = "<?php echo $var; ?>";
document.getElementById('open').text = state;
function toggleText(button_id)
{
if (document.getElementById('button_id').text == "Open")
{
document.getElementById('button_id').text = "Close";
}
else
{
document.getElementById('button_id').text = "Open";
}
}
</script>
<a id="open" onclick="toggleText('open')" href="#"></a>
Thanks a lot, this is the last leg of my project, and for some reason whatever I try wont make it work.
Notice:
I have also tried instead of .text
.innerHTML
and it also did not work.