0

I have a problem with HTML and JS... I made a function with parameter, parameter should be ID of HTML element. But my function take that value, and function getElementById can't read that value. I make a test with console log, and I will show you that on image.

JS Function

function HideShowElementDeleteFolder(id){
  var x = document.getElementById(id);
  console.log(x);
  console.log(id);
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
   x.style.display = "none";
  }
}

Calling JS function in HTML

<a class="text-danger" onclick="HideShowElementDeleteFolder('<?php echo $value; ?>')">
   <i class="fas fa-times"></i>
</a>

HTML Element what I want to show on click

<div id="<?php $value; ?>" class="widget-header clearfix" style="display:none;">
    <form action="" method="post">
        <div>
            <a href="dashboard-files.php?folder=<?php echo $value; ?>">
               <button type="submit" class="btn btn-primary mt-2 mb-2">Obrisi folder</button> 
            </a>
        </div>
    </form>
</div>

enter image description here

Cokenzi
  • 27
  • 4
  • `` doesn’t do anything; you’re missing the `echo`, so no ID is printed. `` isn’t guaranteed to work. Please see [How do I pass variables and data from PHP to JavaScript?](/q/23740548/4642212): use `json_encode`. Inline event handlers like `onclick` are [not recommended](/q/11737873/4642212). They are an [obsolete, hard-to-maintain and unintuitive](/a/43459991/4642212) way of registering events. Always [use `addEventListener`](//developer.mozilla.org/docs/Learn/JavaScript/Building_blocks/Events#inline_event_handlers_%E2%80%94_dont_use_these) instead. – Sebastian Simon Feb 02 '22 at 10:15
  • Yes, it's working now, thank you but, I dont know why that is the problem, beacause the second console.log display correct value. – Cokenzi Feb 02 '22 at 10:21
  • The second `console.log` comes from `onclick="HideShowElementDeleteFolder('')"`, where the `echo` was correct. The first log comes from `document.getElementById(id)`, but there is no element with the ID. Please try using the [debugging capabilities](//ali-dev.medium.com/how-to-easily-debug-in-javascript-5bac70f94f1a) of your browser. The dev tools provide an **Inspector** / **Elements** tab. Inspect your elements. – Sebastian Simon Feb 02 '22 at 10:25

0 Answers0