3

It works... but i want to change the default to hidden. Click first time to show the hidden text

<script>
function displayRow(itemID){
    if ((document.getElementById(itemID).style.display == 'block')) {
        document.getElementById(itemID).style.display = 'display';
    } else { 
        document.getElementById(itemID).style.display = 'display'; 
    }
}
</script>

<button onclick="displayRow('1')">Show/Hidden text</button>
2
  • You can just add style="display: hidden;" to your element, or a class which has that trait by default Commented Oct 29, 2019 at 14:41
  • 1
    document.getElementById(itemID).style.display = 'display'; This doesn't do anything, i.e. display: display is not valid CSS. document.getElementById(itemID).style.display = 'none'; would be better (or use visibility or opacity instead) Commented Oct 29, 2019 at 14:54

3 Answers 3

1

function displayRow(itemID){
    if ((document.getElementById(itemID).style.display == 'none')) {
        document.getElementById(itemID).style.display = 'block';
    } else { 
        document.getElementById(itemID).style.display = 'none'; 
    }
}
<table>
<tr style="display:none" id="1">
<td>Test</td>
</tr>
</table>
<button onclick="displayRow('1')">Show/Hidden text</button>

Use "none" instead "hidden", and put style="display: none" to the element which want to hide by default

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

1 Comment

Perfect! Thank you!
1

Simply set your display to hidden by default

<button style='display: hidden;' onclick="displayRow('<? echo $element ?>')" class='no-print'>Show/Hidden text</button>

and the js

function displayRow(itemID){
    var el = document.getElementById(itemID);
    if ((el.style.display == 'hidden')) {
        el.style.display = 'block';
    } else { 
        el.style.display = 'hidden'; 
    }
}

Comments

0
function displayRow(itemID){
  if (document.getElementById(itemID).style.display == 'block') {
    document.getElementById(itemID).style.display = 'none';
  } else { 
    document.getElementById(itemID).style.display = 'block'; 
  }
}

1 Comment

Welcome to stackoverflow. In addition to the code you've provided, please consider giving a summary as to why and how this solves the issue.

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.