admin管理员组文章数量:1302568
I am using the below code.
function testDisplay(test) {
if (document.getElementById("portests").value == "Hide " + test) {
document.getElementById("portests").value = "Show " + test;
}
else{
document.getElementById("portests").value = "Hide " + test;
}
}
<input type = "button" name = "Test1" id="portests" value = "test1" onclick = "testDisplay(name)">
<input type = "button" name = "Test1" id="portests1" value = "test" onclick = "testDisplay(name)">
<table style="width:100%" id="testing">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr class="test">
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr class="test">
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
<tr class="test">
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
I am using the below code.
function testDisplay(test) {
if (document.getElementById("portests").value == "Hide " + test) {
document.getElementById("portests").value = "Show " + test;
}
else{
document.getElementById("portests").value = "Hide " + test;
}
}
<input type = "button" name = "Test1" id="portests" value = "test1" onclick = "testDisplay(name)">
<input type = "button" name = "Test1" id="portests1" value = "test" onclick = "testDisplay(name)">
<table style="width:100%" id="testing">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr class="test">
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr class="test">
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
<tr class="test">
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
When clicking on button,How to hide/unhide the table(id="testing").rows(class="test")
.
How to call two buttons using the same id value.
Example link: Test
Thanks in advance.
Share edited Dec 14, 2016 at 13:10 Sibeesh Venu 21.8k17 gold badges115 silver badges152 bronze badges asked Dec 14, 2016 at 12:40 robertrobert 111 gold badge1 silver badge5 bronze badges 2- 1 Note that nothing about this question is regarding Java or jQuery. I retagged it to include Javascript for you instead – Rory McCrossan Commented Dec 14, 2016 at 12:41
- 1 it's really unclear what you're asking, please elaborate! – Amin Jafari Commented Dec 14, 2016 at 12:47
4 Answers
Reset to default 4Try this may be help you,
<script>
$(document).ready(function(){
$('#portests').on('click',function(){
$('.test').toggle();
});
});
</script>
<input type = "button" name = "Test1" id="portests" value = "test1" >
<input type = "button" name = "Test1" id="portests1" value = "test" >
<table style="width:100%" id="testing">
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr class="test">
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr class="test">
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
<tr class="test">
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</table>
here is jsfiddle : Jsffidle
If you Need In JAVASCRIPT Try this:
function testDisplay(test) {
if (document.getElementById("portests").value == "Hide " + test) {
document.getElementById("portests").value = "Show " + test;
var table= document.getElementById("testing");
for (var i = 0, row; row = table.rows[i]; i++) {
if(row.className == "test"){row.style.visibility="hidden";}
}
}
else{
document.getElementById("portests").value = "Hide " + test;
var table= document.getElementById("testing");
for (var i = 0, row; row = table.rows[i]; i++) {
if(row.className == "test"){row.style.visibility='visible';}
}
}
}
<input type = "button" name = "Test1" id="portests" value = "test1" onclick = "testDisplay(name)">
<input type = "button" name = "Test1" id="portests1" value = "test" onclick = "testDisplay(name)">
<table width="400px" id="testing">
<thead><tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr></thead>
<tbody>
<tr>
<td>Jill</td>
<td>Smith</td>
<td>50</td>
</tr>
<tr class="test">
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
<tr class="test">
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
<tr class="test">
<td>John</td>
<td>Doe</td>
<td>80</td>
</tr>
</tbody>
</table>
Use a variable called hide. It is set to false
on default. Whenever you click the button the function toggleTable()
will run, this will check the hide
variable and set the display
of the table to ether block
or none
JS
var hide = false;
function toggleTable()
{
if (hide == false)
{
hide = true;
document.getElementById('table').style.display = 'none';
}
else
{
hide = false;
document.getElementById('table').style.display = 'block';
}
}
HTML
<button type="button" onclick="toggleTable()"></button>
<table id="table">
</table>
JS Fiddle: https://jsfiddle/x0wfzdg5/4/
<input type = "button" name = "show" id="show" value = "test1" onclick = "show()">
<input type = "button" name = "hide" id="hide" value = "test" onclick = "hide()">
function show() {
$('#testing').show();
}
function hide(){
$('#settings').hide();
}
本文标签: javascriptHideUnhide table rows clicking on buttonStack Overflow
版权声明:本文标题:javascript - HideUnhide table rows clicking on button? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741671289a2391619.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论