0

I am working on hide show functionality using JavaScript

but it occurring error on console for onclick function

I am not getting why this error occurring.

JAVASCRIPT CODE

<script type="text/javascript">

  function showtabs(id,id1) {
  alert(id);
  alert(id1);
   var e = document.getElementById(id);
   var flag = document.getElementById(id1);
   if(e.style.display == 'block')
   {
      e.style.display = 'none';
      flag.innerHTML = '+';
   }
   else
   {
      e.style.display = 'block';
      flag.innerHTML = '-';
   }  
}

PHP CODE

<?php

$output.='<div class="span6"> 
  <div class="header" onclick="showtabs(my_section,icon);">
  <h1>My Section <span class="iconc" id="icon">+</span></h1>
  </div></div>';
$output.='<div id="my_section" style="display:none;">This is my section</div>';
echo $output;

?>

Error Log

TypeError: e is null
if(e.style.display == 'block')
3
  • What about my_section Commented Jun 9, 2014 at 9:59
  • are you able to get the ids inside showtabs? Commented Jun 9, 2014 at 10:00
  • no....it gives me alert like [object HTMLDivElement] Commented Jun 9, 2014 at 10:02

5 Answers 5

1

Try this:

<html>
<?php 
$output='';

$output.='<div class="span6"> 
  <div class="header" onclick="showtabs(\'my_section\',\'icon\');">
  <h1>My Section <span class="iconc" id="icon">+</span></h1>
  </div></div>';
$output.='<div id="my_section" style="display:none;">This is my section</div>';

?>
<head>
    <title>Sample Eula Agreement</title>
    <script type="text/javascript">

  function showtabs(id,id1) {
  alert(id);
  alert(id1);
   var e = document.getElementById(id);
   var flag = document.getElementById(id1);

   alert(e.style.display);
   if(e.style.display == 'block')
   {
      e.style.display = 'none';
      flag.innerHTML = '+';
   }
   else
   {
      e.style.display = 'block';
      flag.innerHTML = '-';
   }  
}

</script>
<style>

</style>

</head>
<body>
<?php


echo $output;

?>

</body>
</html>

You are passing string inside your function but because of the missing quotes, It is reading those as HTML elements. Thats why, e is coming null.

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

4 Comments

if i adding line above it gives Parse error: syntax error, unexpected T_STRING
See the whole code, which I am able to run successfully and it is working perfectly fine.
but i want to display HTML in php only like i have added code sample above
Got your point. See the updated code. If you have further issue, feel free to let me know.
0

Looks like the variable e is null. In other words there is no element in the dom with the id that you are passing to the function.

Comments

0

You forgot the quotes. Try this: onclick="showtabs('my_section','icon');"

Comments

0

I think you are not selecting id and id1 perfectly

var e = document.getElementById('id');   //check here
var flag = document.getElementById('id1');

1 Comment

because it takes parameter as string document.getElementById(elementID) //elementID is string
0

just change the code of php of $output variable onclick=showtabs("my_section","icon") because you pass the id without double quats, So its react like object

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.