473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

hide/unhide script for dyanamic table

Hi everyone,

I have found and addapted a script that displays or hides a div tag
based on it's current class tag, but i can't seem to get it to work. I
am new to Java, and i know that everything has to be perfect in order
for it to function.

Would it be possible for someone to check over my script and point out
any errors there may be/extra spaces etc?

Here is the script:

function Toggle(id)
{
if(document.getElementById("details" + id).class == 'hide')
{
document.getElementById("details" + id").class='blue_sub_body_exp';
document.images["expand" + id].src= "colapse.gif";
}

else
{
document.getElementById("details" + id).class='hide';
document.images["expand" + id].src= "expand.gif";
}

}

Thanks, i appologise if it's something glaringly obvious.

Gavin

Feb 3 '07 #1
3 5679
Daz
On Feb 3, 10:13 pm, "Gavin" <GJPeac...@gmail.comwrote:
Hi everyone,

I have found and addapted a script that displays or hides a div tag
based on it's current class tag, but i can't seem to get it to work. I
am new to Java, and i know that everything has to be perfect in order
for it to function.
I Gavin. First of all it's 'JavaScript', not Java. It's a common
misconception that the wo are related, when they actually aren't.
>
Would it be possible for someone to check over my script and point out
any errors there may be/extra spaces etc?
I can't see any errors in your script, but I am not an expert. Are you
getting any errors? I recommend you install Firefox, and use the
JavaScript debugger which will tell you if anything is wrong. I
suspect that you might not be calling on the function properly. Can
you post an example of the actual page that contains the onclick
function trigger?
>
Here is the script:

function Toggle(id)
{
if(document.getElementById("details" + id).class == 'hide')
{
document.getElementById("details" + id").class='blue_sub_body_exp';
document.images["expand" + id].src= "colapse.gif";

}

else
{
document.getElementById("details" + id).class='hide';
document.images["expand" + id].src= "expand.gif";

}
}

Thanks, i appologise if it's something glaringly obvious.

Gavin

Feb 3 '07 #2
Gavin wrote:
Hi everyone,

I have found and addapted a script that displays or hides a div tag
based on it's current class tag, but i can't seem to get it to work. I
am new to Java,
Javascript is not Java. :-)
and i know that everything has to be perfect in order
for it to function.

Would it be possible for someone to check over my script and point out
any errors there may be/extra spaces etc?

Here is the script:

function Toggle(id)
It's a common convention that function names starting with an uppercase
letter are constructors. Better in this case to use a lower case "t".

{
if(document.getElementById("details" + id).class == 'hide')
Since "class" is a future reserved word in ECMAScript ed 3 (and likely
to be used if JavaScript 2.0 aka ECMAScript ed 4 is ever released) the
CSS class property is called "className".

It is also prudent to check that the value returned by getElementById
before attempting to access its class property:

function toggle(id) {
var el = document.getElementById("details" + id);
if (el && el.className == 'hide') {
...

{
document.getElementById("details" + id").class='blue_sub_body_exp';
If you store the returned value of the getElementById, then you don't
have to call it again.

el.className = 'blue_sub_body_exp';

document.images["expand" + id].src= "colapse.gif";
You can probably change the image too as a property of the CSS class
applied to the div - look at using a positioned background image with no
repeat, then when you change the class of the div, the image changes
too. Then your toggle function becomes:

function toggle(id) {
var el = document.getElementById("details" + id);
if (el) {
el.className = (el.className=='hide')? 'blue_sub_body_exp':'hide';
}
}
--
Rob
Feb 4 '07 #3
Daz
On Feb 4, 1:30 am, RobG <r...@iinet.net.auwrote:
Gavin wrote:
Hi everyone,
I have found and addapted a script that displays or hides a div tag
based on it's current class tag, but i can't seem to get it to work. I
am new to Java,

Javascript is not Java. :-)
and i know that everything has to be perfect in order
for it to function.
Would it be possible for someone to check over my script and point out
any errors there may be/extra spaces etc?
Here is the script:
function Toggle(id)

It's a common convention that function names starting with an uppercase
letter are constructors. Better in this case to use a lower case "t".
{
if(document.getElementById("details" + id).class == 'hide')

Since "class" is a future reserved word in ECMAScript ed 3 (and likely
to be used if JavaScript 2.0 aka ECMAScript ed 4 is ever released) the
CSS class property is called "className".

It is also prudent to check that the value returned by getElementById
before attempting to access its class property:

function toggle(id) {
var el = document.getElementById("details" + id);
if (el && el.className == 'hide') {
...
{
document.getElementById("details" + id").class='blue_sub_body_exp';

If you store the returned value of the getElementById, then you don't
have to call it again.

el.className = 'blue_sub_body_exp';
document.images["expand" + id].src= "colapse.gif";

You can probably change the image too as a property of the CSS class
applied to the div - look at using a positioned background image with no
repeat, then when you change the class of the div, the image changes
too. Then your toggle function becomes:

function toggle(id) {
var el = document.getElementById("details" + id);
if (el) {
el.className = (el.className=='hide')? 'blue_sub_body_exp':'hide';
}

}

--
Rob
Doh! It's className not class. How did I miss that...? I think my
skills of observation have a lot to be desired.

Feb 4 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Wang, Jay | last post by:
I try to group several rows in a table into a div and show/hide them by click on a button somewhere with a javascript link. When clicked, the link will toggle the style of the div section's style...
5
by: mooeypoo | last post by:
Hello all, I'm a php developer. I have been using a very simple script to hide/unhide divs in my script: function DisplayI(obj) { obj.style.display=("none"==obj.style.display? "block" :...
2
by: | last post by:
I have a page where I have 3 combo boxes listed in a column. sort of like: combo1 combo2 combo3 Based on which one is clicked, the others are supposed to hide (i.e. combobox.visible =...
3
by: toodi4 | last post by:
I'm using a javascript that hides and unhides text based on a button click. It works great across static fields on a form. The problem I have is that I'm trying to hide and unhide various fields...
3
by: =?Utf-8?B?aWxy?= | last post by:
Hi All I am developing an vb 2005 winforms application that connects to a database and displays a datagridview of the data. A user can select and then edit, or create a new entry by opening a...
1
by: Simon | last post by:
Dear reader, In a datasheet form you have the possibility to hide or unhide columns. But in case the datasheet form is a sub-form this functionality is not longer available.
7
by: cefrancke | last post by:
I have a few tables with sensitive user information (passwords, etc.) and I would like to prevent someone from opening a blank database and importing those tables. Is there a way to "hide" or...
4
by: somasekhar | last post by:
Does anyone know how to have a user click a checkbox to unhide a editfield Unclick of the checkbox would hide it again. There is an example of html file on the Javascript .But I want hide and unhide...
6
by: Ralph | last post by:
Hi, I was reading effictive C++ and some other books again and they all tell you about hiding implementation details (proxy/pimpl/inheritance) but they never really explain when to use it. I...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.