html - Javascript toggle button that changes div visibility, and toggles button text - Stack Overflow

This would be a great deal easier if I could use jQuery, but I'm trying to avoid doing so for a fe

This would be a great deal easier if I could use jQuery, but I'm trying to avoid doing so for a few reasons. Meanwhile, I've gotten some of the script to work - and I'm attempting to add the function to toggle the button text with the state of the div.

<script type="text/javascript">
function toggleMe(a){
    var e = document.getElementById(a);
    if(!e) return true;
    if(e.style.display == "none") {
        e.style.display = "block"
    } else {
        e.style.display = "none"
    }
    return true;
}
</script>

That script works, and nicely, with my current button:

<input type="button" onclick="return toggleMe('para0')" value="Expanse"><br> 
<div id="para0">TEST TEXT</div>

Trying to have the text toggle with the state of the div visibility. Most answers I've seen are using jQuery, and the context I'm putting the script into makes forms a non-option.

This would be a great deal easier if I could use jQuery, but I'm trying to avoid doing so for a few reasons. Meanwhile, I've gotten some of the script to work - and I'm attempting to add the function to toggle the button text with the state of the div.

<script type="text/javascript">
function toggleMe(a){
    var e = document.getElementById(a);
    if(!e) return true;
    if(e.style.display == "none") {
        e.style.display = "block"
    } else {
        e.style.display = "none"
    }
    return true;
}
</script>

That script works, and nicely, with my current button:

<input type="button" onclick="return toggleMe('para0')" value="Expanse"><br> 
<div id="para0">TEST TEXT</div>

Trying to have the text toggle with the state of the div visibility. Most answers I've seen are using jQuery, and the context I'm putting the script into makes forms a non-option.

Share Improve this question edited Dec 29, 2012 at 22:31 Andrew Hubbs 9,4349 gold badges50 silver badges71 bronze badges asked Dec 29, 2012 at 21:14 KyleKyle 431 gold badge1 silver badge3 bronze badges 1
  • The real trick is trying to do this will plain ol' javascript. The context I'm using this with is going to be very simple and has to be modular, so I want to keep it as self-contained as I can. – Kyle Commented Dec 29, 2012 at 21:54
Add a ment  | 

2 Answers 2

Reset to default 1

See if this helps:

HTML:

<button id="toggle">Toggle</button>
<div id="text">Lorem ipsum dolor sit amet.</div>

JavaScript:

var button = document.getElementById('toggle'),
    text = document.getElementById('text');

button.onclick = function() {
  var isHidden = text.style.display == 'none';
  text.style.display = isHidden ? 'block' : 'none';
};

Demo: http://jsbin./emikux/1/edit

My JS is a little rusty and I can't check this right now, but I think somthing like this will do what you want:

<input type="button" onclick="return toggleMe(this,'para0')" value="Expanse"><br> 
<div id="para0">TEST TEXT</div>

<script type="text/javascript">
    function toggleMe(btn,a){
    var e=document.getElementById(a);
    if(!e)return true;
    if(e.style.display=="none"){
      e.style.display="block";
      btn.value = "Hide";
   }
   else{
      e.style.display="none";
      btn.value = "Show";
   }
   return true;
  }
  </script>

Fixed the spacing show /script would show.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745366650a4624611.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信