| | |
Using Java have a table "hidden" onload of page
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Oct 2006
Posts: 19
Reputation:
Solved Threads: 0
I have what I hope is a simple question.
I found a js that does exactly what I want, except that I want the table 'closed' when the page is loaded, and opened when the +/- is clicked. I'd like this in Java. (assuming that non-java users would see the table 'open/show')
Hope someone can help. Thanks
Here is the script:
I found a js that does exactly what I want, except that I want the table 'closed' when the page is loaded, and opened when the +/- is clicked. I'd like this in Java. (assuming that non-java users would see the table 'open/show')
Hope someone can help. Thanks
Here is the script:
java Syntax (Toggle Plain Text)
function show_hide(tblid, show) { if (tbl = document.getElementById(tblid)) { if (null == show) show = tbl.style.display == 'none'; tbl.style.display = (show ? '' : 'none'); } }
html Syntax (Toggle Plain Text)
<a href="javascript:void();" onclick="show_hide('exampletbl')">+/-</a> <table id="exampletbl"> <tbody> <tr> <td>row 1</td> </tr> <tr> <td>row 2</td> </tr> </tbody> </table> <br> <a href="javascript:void();" onclick="show_hide('exampletbl2')">+/-</a> <table id="exampletbl2"> <tbody> <tr> <td>row 3</td> </tr> <tr> <td>row 4</td> </tr> </tbody> </table>
•
•
Join Date: Oct 2006
Posts: 19
Reputation:
Solved Threads: 0
Think I solved my own problem - found this thread and combined that with what I was already doing.
My Java now looks like this. New stuff is on lines 10-12
Add this one line to html
Works like a charm. Both table hidden when page loaded, opened when I click.
My Java now looks like this. New stuff is on lines 10-12
java Syntax (Toggle Plain Text)
function show_hide(tblid, show) { if (tbl = document.getElementById(tblid)) { if (null == show) show = tbl.style.display == 'none'; tbl.style.display = (show ? '' : 'none'); } } function hide(id) { document.getElementById(id).style.display = 'none'; }
Add this one line to html
html Syntax (Toggle Plain Text)
<body onload="hide('exampletbl'); hide('exampletbl2');">
Works like a charm. Both table hidden when page loaded, opened when I click.
BBQKaren,
First and foremost - Javascript and Java are completely different languages. Google "java versus javascript" for many detailed discussions and explanations.
For your problem, you need code not only to hide the tables initially, but also to generate the +/- controls. Otherwise the small minority with javascipt disabled will see controls that do nothing.
In the following code, an anonymous function which fires when the page has loaded, loops through the array
The only other thing you need to do is to get rid of the +/- controls from your HTML and wrap the tables in <div>s.
As you will see, I have styled these divs to give some vertical space above. You can modify this aspect as required.
Hope this helps.
Airshow
First and foremost - Javascript and Java are completely different languages. Google "java versus javascript" for many detailed discussions and explanations.
For your problem, you need code not only to hide the tables initially, but also to generate the +/- controls. Otherwise the small minority with javascipt disabled will see controls that do nothing.
In the following code, an anonymous function which fires when the page has loaded, loops through the array
hideTables (composition to be determined by you) calling my function initHiddenTables as it goes. initHiddenTables creates the necessary +/- control for one specified table then hides that table. javascript Syntax (Toggle Plain Text)
function initHiddenTable(tableId){ var tbl = document.getElementById(tableId); if(tbl) { //1. Generate +/- control var a = document.createElement('a'); a.setAttribute('href', ''); a.onclick = function(){ show_hide(tableId); return false; } var txt = document.createTextNode('+/-'); a.appendChild(txt); tbl.parentNode.insertBefore(a, tbl); //2. Hide Table if(tbl) { tbl.style.display = 'none'; } } } onload = function(){ var hideTables = ["exampletbl", "exampletbl2"]; for(var i=0; i<hideTables.length; i++){ initHiddenTable(hideTables[i]); } }
The only other thing you need to do is to get rid of the +/- controls from your HTML and wrap the tables in <div>s.
html Syntax (Toggle Plain Text)
<div style="margin-top:12px;"> <table id="exampletbl"> <tbody> <tr><td>row 1</td></tr> <tr><td>row 2</td></tr> </tbody> </table> </div> <div style="margin-top:12px;"> <table id="exampletbl2"> <tbody> <tr><td>row 3</td></tr> <tr><td>row 4</td></tr> </tbody> </table> </div>
Hope this helps.
Airshow
Last edited by Airshow; Jul 27th, 2009 at 8:22 pm.
50% of the solution lies in accurately describing the problem!
•
•
Join Date: Oct 2006
Posts: 19
Reputation:
Solved Threads: 0
for some reason, when you toggle the plain text (on this forum), the item below inlcudes an extra <b></b>. This is not in my real document.
•
•
•
•
I have what I hope is a simple question.
Ihtml Syntax (Toggle Plain Text)
<a href="javascript:void();" onclick="show_hide('exampletbl')">+/
It's either a bug or, more probably, an anti code injection measure.
But it should be completely academic, because nobody should ever need to use that syntax. Javascript URLs are very 1990s.
See my reponse to your question above.
Airshow
But it should be completely academic, because nobody should ever need to use that syntax. Javascript URLs are very 1990s.
See my reponse to your question above.
Airshow
50% of the solution lies in accurately describing the problem!
![]() |
Similar Threads
- how can I keep "checked" boxes within div to default to "unchecked" if div is"hidden" (JavaScript / DHTML / AJAX)
- IF hidden field is NULL then hidden field is "blah" (PHP)
- Firefox Compatibility help with script (JavaScript / DHTML / AJAX)
- "each new axis love", "bodyplatformplatform" and "modefrag error" (Viruses, Spyware and other Nasties)
- "topantispyware" virus (Viruses, Spyware and other Nasties)
- google "keyword" question (Search Engine Optimization)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Javascript on Firefox
- Next Thread: Javascript Permission Denied Problem
| Thread Tools | Search this Thread |
Tag cloud for JavaScript / DHTML / AJAX
ajax ajaxcode ajaxhelp ajaxjspservlets animate api automatically blackjack browser bug calendar captchaformproblem checkbox child class close cookies createrange() cursor dependent disablefirebug dom dropdown editor element engine events explorer ext file firehose flash form forms game gears google gxt hiddenvalue highlightedword html htmlform ie8 iframe image() images internet java javascript jawascriptruntimeerror jquery jsf jsfile jump libcurl math matrixcaptcha media microsoft mp3 mysql object offline onerror onmouseoutdivproblem onreadystatechange parent passing pdf php player post progressbar rated regex runtime scroll search session shopping size software solutions sql star stars stretch synchronous text textarea twitter unicode validation web website window windowsxp wysiwyg xspf \n





