0

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.

1
  • your script is running before the dom is ready, either use an onload event or put the script at the end of the body. Also its innerText,innerHTML,or textContent not text Commented Aug 31, 2014 at 23:40

4 Answers 4

1
<script type="text/javascript">
document.getElementById('buttonid').innerHTML = "<?php echo $var; ?>";
function toggleText(button) 
{
   if (button.innerHTML == "Open") 
   {
       button.innerHTML = "Close";
   }
   else 
   {
     button.innerHTML = "Open";
   }
}
</script>
<a id="buttonid" onclick="toggleText(this)" href="#"></a>

Put script after </body> tag

Sign up to request clarification or add additional context in comments.

Comments

1

Have you checked the console? It's always the place to start.

Part of the problem is that you have button_id in single quotes. I assume that button_id is a variable name, and should not be in quotes. Also, you should be using innerHTML instead of text.

This should work:

<script type=.innerHTML/javascript">
var state =  "<?php echo $var; ?>";
document.getElementById('open').innerHTML = state;
function toggleText(button_id) 
{
   if (document.getElementById(button_id).innerHTML == "Open") 
   {
       document.getElementById(button_id).innerHTML = "Close";
   }
   else 
   {
     document.getElementById(button_id).innerHTML = "Open";
   }
}
</script>
<a id="open" onclick="toggleText('open')" href="#"></a>

Comments

0

try calling document.getElementById('open').innerText= state; on window.onload

window.onload=function(){
     document.getElementById('open').innerText= state;
}

The problem could be that the dom is not ready (the page is not fully loaded) when you try to set the text property

Comments

0

Try this: <a id="button_id" onclick="toggleText('open')" href="#">open</a> And put the script just before the closing </body> tag.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.