0

I am trying to do the following:

<html>
<script type="text/javascript" src="jquery.js"></script>

<a id="random">before</a>   
<script>
function test(a)
  {
      document.getElementById('random').innerHTML="after with variable passed in";
  }
function test2()
  {
      document.getElementById('random').innerHTML="after without variable passed in";
  }
</script>
<?php $sample = "hi"; ?>

<button onClick="test(<?php echo $sample;?>)">withVariable</button>

<button onClick="test2()">withoutVariable</button>
</html>

If I click the "withoutVariable" button, the function test2() gets called perfectly because no variables are passed in to it. However, if I click the "withVariable" button the function test(a) is never being called for some reason. Any ideas what I'm doing wrong here?

2 Answers 2

3

Since $sample is a string literal you need to enclose it with ''

<button onClick="test('<?php echo $sample;?>')">withVariable</button>
Sign up to request clarification or add additional context in comments.

1 Comment

Wow, I wish I had asked earlier.. Such a small mistake
0

If you're passing a string variable, then you need to add quotes around the value, like so:

<button onClick="test('<?php echo $sample;?>')">withVariable</button>

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.