最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

showing or hiding div with id generated using php and javascript - Stack Overflow

programmeradmin3浏览0评论

I am trying to hide or show the div based on the div id from the php function. I am not able to make it work, please help me.

Javascript:

<script>
showOrHide(id) {
    var elem=getElementById(id);
    if(elem.style.visibility="hidden")
        elem.style.visibility="visible";
    else
        elem.style.visibility="hidden";
}
</script>

PHP Script:

<?php
function display_link($link_id,$upvote_array,$downvote_array,$divid) {
?>
    <a href="javascript:showOrHide(<?php echo $divid; ?>)">More links</a>
    <div id="<?php echo $divid ?>" style="visibility:hidden;">
    </div>
<?php } ?>

I am trying to hide or show the div based on the div id from the php function. I am not able to make it work, please help me.

Javascript:

<script>
showOrHide(id) {
    var elem=getElementById(id);
    if(elem.style.visibility="hidden")
        elem.style.visibility="visible";
    else
        elem.style.visibility="hidden";
}
</script>

PHP Script:

<?php
function display_link($link_id,$upvote_array,$downvote_array,$divid) {
?>
    <a href="javascript:showOrHide(<?php echo $divid; ?>)">More links</a>
    <div id="<?php echo $divid ?>" style="visibility:hidden;">
    </div>
<?php } ?>
Share Improve this question edited Apr 9, 2013 at 5:16 Subedi Kishor 5,9965 gold badges36 silver badges53 bronze badges asked Apr 9, 2013 at 4:50 AravindAravind 1,4011 gold badge18 silver badges41 bronze badges 4
  • 1 in what way does it not work? most people would jquery to do this – user557846 Commented Apr 9, 2013 at 4:52
  • 1 You JavaScript code is syntaxically invalid. jshint. – plalx Commented Apr 9, 2013 at 4:53
  • You're missing the closing bracket from the php function, also showOrHide needs to be function showOrHide(id) {} – francisco.preller Commented Apr 9, 2013 at 4:53
  • You might want to clean up your JS - using a Linter like jslint. can be useful for doing it on the fly. – Scott Sword Commented Apr 9, 2013 at 4:54
Add a ment  | 

3 Answers 3

Reset to default 4

Here's a cleaned-up version of your function.

function showOrHide(id) {

  var elem = document.getElementById(id);

  elem.style.visibility = (elem.style.visibility === 'hidden')? 'visible' : 'hidden';
}
<script>
function showOrHide(id){
 var elem = getElementById(id);
 if(elem.style.visibility=="hidden"){
  elem.style.visibility="visible";
 } else {
  elem.style.visibility="hidden";
 }
}
</script>
<?php
function display_link($link_id,$upvote_array,$downvote_array,$divid)
{
?>
<a href="javascript:showOrHide('<?php echo $divid; ?>')">More links</a>
<div id="<?php echo $divid ?>" style="visibility:hidden;">
</div>

1 - visibility == 'hidden'

2 - missing ; after visibility="visible" and ="hidden"

3 - href="javascript:showOrHide('')" - missing '' inside your javascript function

Try this

 <script>
    function showOrHide(id){
     var elem = document.getElementById(id);

  elem.style.visibility = (elem.style.visibility === 'hidden')? 'visible' : 'hidden';
    }

    </script>
    <?php
    function display_link($link_id,$upvote_array,$downvote_array,$divid)
    {
    ?>
    <a  href="javascript:showOrHide(<?php echo $divid; ?>)">More links</a>
    <div id="<?php echo $divid ?>" style="display:hidden;">
    </div>
    <?php
    }
    ?>
发布评论

评论列表(0)

  1. 暂无评论