1

I'm trying to call a js function from the OnClick within multiple DIVs, it passes a 'Section Name' secOther001, 002, 003 etc...'.

<?php
$num=mysql_numrows($result);

$i=0;
while ($i < $num) {

$TaskID=sprintf("%03d",mysql_result($result,$i,"TaskID"));
$secOthersID = "SecOther$TaskID";
echo "
<div class='clTaskSection' onclick='SecOtherVisibility($secOthersID)'>...</div>
<div id='$secOthersID' class='clTaskSection'>content...</div>";

$i++;
}

?>

it should then call the js below, providing the relevant section name:

<script>
function SecOtherVisibility(divName) {
    if (document.getElementById(divName).style.display = "none") {
      document.getElementById(divName).style.display = "block"
    } else {
      document.getElementById(divName).style.display = "none";
    }
}
</script>

But I get Uncaught TypeError: Cannot read property 'style' of null. Where am I going wrong?

6
  • 1
    What's PHP got to do with this? Commented Nov 7, 2015 at 23:22
  • Have you considered using jQuery? Commented Nov 7, 2015 at 23:22
  • Where is $secOthersID defined? Commented Nov 7, 2015 at 23:22
  • PHP Pulls records from a MySQL DB to create nested DIVs with the record data. PHP defines $secOthersID Commented Nov 7, 2015 at 23:31
  • Typo error: == needed in the if ;) Commented Nov 7, 2015 at 23:36

3 Answers 3

1

More importantly, look at the rendered result:

... onclick='SecOtherVisibility(SecOther001)' ...

Do you see the problem?

How about now:

... onclick='SecOtherVisibility(<?=json_encode($secOthersID)?>)' ...

Outputs:

... onclick='SecOtherVisibility("SecOther001")' ...

I hope that clears things up!

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

3 Comments

Now I just get [Uncaught SyntaxError: Unexpected token <]
Updated with additional info... <DIV>s are within an echo"..."
Then just close the quotes - 'SecOtherVisibility(".json_encode($secOthersID).");'
1

Change it to this:

<div class='clTaskSection' onclick='SecOtherVisibility(<?= $secOthersID ?>)'>...</div>

You can't just use PHP variables unless in PHP context.

4 Comments

Now I just get [Uncaught SyntaxError: Unexpected token <]
Might not have shorthand enabled try: <?php echo $secOthersID ?> instead of <?= $secOthersID ?>
Ahh I think that's throwing everyone who has answered you off.
Updated with additional info... <DIV>s are within an echo"..."
1

The function expects a string as divName. You are passing the variable $secOthersID, which is undefined. Your HTML needs to be something like:

<div class='clTaskSection' id="cats" onclick='SecOtherVisibility("cats")'>Contents of div...</div>

5 Comments

Thanks for the suggestion, but this doesn't work as it has to pass a dynamic value, will try MiDri's example.
Sorry. I used the wrong quotation marks. Thanks to @SashaPachev for the fix.
Note that secOthersID is not undefined as if you check the source it is 'secOther001', 'secOther002' etc...
@aSystemOverload But that's part of the PHP, which never reaches the user. Could you post a more complete section of you PHP script? Are the <input />s generated in a loop?
Updated with additional info... <DIV>s are within an echo"..."

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.