DDDD
DDDD

Reputation: 3940

Javascript to show/hide div of render

Ruby on Rails 3. I am trying to get a button to show or hide a div.

This is not returning any errors but nothing happens.

<input type=button value="Show Archived Postings" onclick="showHide('oldNews');">

<div id="oldNews">
<%= render 'archive_news' %>
</div>

<script language="javascript" type="text/javascript">
function showHide(elementId) {
    if (document.getElementById) {
        var element = document.getElementById(elementId);
        if (element.style.visibility == 'visible') {
            element.style.visibility = 'hidden';
        } else if (element.style.visibility == 'hidden') {
            element.style.visibility = 'visible';
        }
    }
}
</script>

It will always render my 'archive_news'. What am I doing wrong? Thank you

Upvotes: 0

Views: 1650

Answers (1)

Suman Bogati
Suman Bogati

Reputation: 6351

You can use this code for your requirement. You can use style property for hide and display element when you click on button.

//this would hide when body is loaded
var element = document.getElementById('oldNews');
if(element != null){
    element.style.display = 'none';
}

//this would hide when you click on input button
function showHide(elementId) {
  var element = document.getElementById(elementId);
    if (element != null) {
        if(element.style.display != 'none'){
            element.style.display = 'none';
        }else{
            element.style.display = 'block';
        }
    }
}

Upvotes: 1

Related Questions