473,387 Members | 1,790 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,387 software developers and data experts.

Help with naming elements in a loop

I have a switch statement that controls which of several containers is
displayed or not. It currently looks like:

function showHelp(n) {
show('vhelp'); //makes parent container visible
switch (n){
case 0:
show('image0');hide('image1');hide('image2');...;h ide('imagem');
break;
..
..
case m:
show('imagem');hide('image0');...;hide('imagem-1');
}

The functions show('target') and hide('target') are simple js functions,
i.e., : document.getElementById(target).style.display = "" //(or "none")

I want to change the lengthy show/hide strings. I've tried using
variations to name the appropriate containers in the function:

function xImages (n) {
for(var i=0; i <m+1; i++) { //I hard enter m+1
if (i==n) {document.form1.elements["image"+(i)].style.display="";}
else {document.form1.elements["image"+(i)].style.display='none';}
}

I get the error msg message:
Could not convert undefined or null to object
document.form1.elements[("image" + i)].style.display = "none";

Apparently I'm not addressing the container properly, but I can't comeup
with the correct syntax. HELP! Please.
--
Ed Jay (remove 'M' to respond by email)
Aug 14 '06 #1
5 1429
Ed Jay wrote:
I have a switch statement that controls which of several containers is
displayed or not. It currently looks like:

function showHelp(n) {
show('vhelp'); //makes parent container visible
switch (n){
case 0:
show('image0');hide('image1');hide('image2');...;h ide('imagem');
break;
..
..
case m:
show('imagem');hide('image0');...;hide('imagem-1');
}

The functions show('target') and hide('target') are simple js functions,
i.e., : document.getElementById(target).style.display = "" //(or "none")
I am unsure if the empty string is a valid value for display.
Try 'inline' for span-like elements (elements that flow with the tekst)
or use 'block' for div-like elements.
>
I want to change the lengthy show/hide strings. I've tried using
variations to name the appropriate containers in the function:

function xImages (n) {
for(var i=0; i <m+1; i++) { //I hard enter m+1
if (i==n) {document.form1.elements["image"+(i)].style.display="";}
else {document.form1.elements["image"+(i)].style.display='none';}
}

I get the error msg message:
Could not convert undefined or null to object
document.form1.elements[("image" + i)].style.display = "none";
I think you better start with checking what your form contains.
It strikes me that you address your images via your form...
You usually use document.images.etc to address images.
Or when they are labeled with an id, getElementById().
My advise:
- put all images you want to be able to show/hide in seperate divs, naming
the divs like this:
<div id="imgdiv0" style="display:block">
<img src="images/img0.gif">
</div>

Or use span if that is more appropriate.

Now address the div instead of a formelement, which it is not, like this:
var myRef = document.getElmentById("imgdiv0");
myRef.style.display = "none";
or
myRef.style.display = "block";

In case of span use 'inline' instead of 'block'.

Hope that helps,

Regards,
Erwin Moller
>
Apparently I'm not addressing the container properly, but I can't comeup
with the correct syntax. HELP! Please.
Aug 15 '06 #2
Erwin Moller wrote:
Ed Jay wrote:
<snip>
>The functions show('target') and hide('target') are simple js functions,
i.e., : document.getElementById(target).style.display = "" //(or "none")

I am unsure if the empty string is a valid value for display.
<snip>

An empty string is a valid value to assign to any property of an
element's - style - object that is expecting a string type (almost all
except zIndex). As the - style - object reflects the values assigned
using the STYLE attribute in HTML assigning an empty string is
equivalent to removing a CSS declaration from the STYLE attribute, so
the applied style becomes those derived from external style sheets or
the browser's default.

Assigning an empty string is in fact a useful facility where specific
elements have various values depending on the browser. For example, a
TD element may be - display:table-cell; - on modern browsers and -
display:block - on IE, and setting 'block' were 'table-cell' is
expected produces unexpected effects. So switching a TD from -
display:none; - to a state where it is displayed either requires
knowing (or working out) which value actually applies on the browser in
question, or assigning a value that removes the 'none' value and allows
the element to revert to its default. for which an empty string is an
ideal value.
You usually use document.images.etc to address images.
Or when they are labeled with an id, getElementById().
<snip>

The W3C HTML DOM specification requires that IMG elements can be
referenced in the document.images collection with their ID attributes
as well as by NAME attribute. So even if the IMG only has an ID
attribute it is still not necessary to use - getElementById -.

Richard.

Aug 15 '06 #3
Erwin Moller scribed:
>Ed Jay wrote:
>I have a switch statement that controls which of several containers is
displayed or not. It currently looks like:

function showHelp(n) {
show('vhelp'); //makes parent container visible
switch (n){
case 0:
show('image0');hide('image1');hide('image2');...; hide('imagem');
break;
..
..
case m:
show('imagem');hide('image0');...;hide('imagem-1');
}

The functions show('target') and hide('target') are simple js functions,
i.e., : document.getElementById(target).style.display = "" //(or "none")

I am unsure if the empty string is a valid value for display.
Try 'inline' for span-like elements (elements that flow with the tekst)
or use 'block' for div-like elements.
An empty string is valid.
>
>I want to change the lengthy show/hide strings. I've tried using
variations to name the appropriate containers in the function:

function xImages (n) {
for(var i=0; i <m+1; i++) { //I hard enter m+1
if (i==n) {document.form1.elements["image"+(i)].style.display="";}
else {document.form1.elements["image"+(i)].style.display='none';}
}

I get the error msg message:
Could not convert undefined or null to object
document.form1.elements[("image" + i)].style.display = "none";

I think you better start with checking what your form contains.
It strikes me that you address your images via your form...
You usually use document.images.etc to address images.
Or when they are labeled with an id, getElementById().
I'm not addressing images. I'm addressing containers <div name="image1">.
>
My advise:
- put all images you want to be able to show/hide in seperate divs, naming
the divs like this:
<div id="imgdiv0" style="display:block">
<img src="images/img0.gif">
</div>
Precisely what I'm doing.
>
Or use span if that is more appropriate.

Now address the div instead of a formelement, which it is not, like this:
var myRef = document.getElmentById("imgdiv0");
myRef.style.display = "none";
or
myRef.style.display = "block";

In case of span use 'inline' instead of 'block'.

Hope that helps,
It did, thank you.
>>
Apparently I'm not addressing the container properly, but I can't comeup
with the correct syntax. HELP! Please.
The code ended up as (which eliminated the switch statement entirely):

function xImages(n) {
for(var i=0; i <m-1; i++) {
f (i==n) {document.getElementById(["image"+(i)]).style.display = "";}
else {document.getElementById(["image"+(i)]).style.display = "none";}
}
Thanks all for the help.
--
Ed Jay (remove 'M' to respond by email)
Aug 15 '06 #4
JRS: In article <0n********************************@4ax.com>, dated
Mon, 14 Aug 2006 12:01:42 remote, seen in news:comp.lang.javascript, Ed
Jay <ed***@aes-intl.composted :
>I have a switch statement that controls which of several containers is
displayed or not. It currently looks like:

function showHelp(n) {
show('vhelp'); //makes parent container visible
switch (n){
case 0:
show('image0');hide('image1');hide('image2');...;h ide('imagem');
break;
..
..
case m:
show('imagem');hide('image0');...;hide('imagem-1');
}

The functions show('target') and hide('target') are simple js functions,
i.e., : document.getElementById(target).style.display = "" //(or "none")
Presumably you mean that the above works with show & hide.

Replace the switch with
for (j=0; j<=m; j++) (j==n ? show : hide)("image"+j) ;
maybe?

if (i==n) {document.form1.elements["image"+(i)].style.display="";}
else {document.form1.elements["image"+(i)].style.display='none';}
Those can be written as
if (i==n)
{document.form1.elements["image"+i].style.display = i==n ? "" : "none"

Untested.

aside: !! i should not be used as a variable in News,
!! because a good spellchecker will complain.
It could be more elegant to save in a variable the number of what is
currently shown. Then the change routine would only need to hide that
and show the new one.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 15 '06 #5
Dr John Stockton said the following on 8/15/2006 1:03 PM:

<snip>
aside: !! i should not be used as a variable in News,
!! because a good spellchecker will complain.
Not if the person using it is wise enough to tell it to ignore it.
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 15 '06 #6

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

Similar topics

4
by: VK | last post by:
09/30/03 Phil Powell posted his "Radio buttons do not appear checked" question. This question led to a long discussion about the naming rules applying to variables, objects, methods and properties...
6
by: MAFranklin | last post by:
I've created a form with several input fields. At the beginning of my form I use a set of radio buttons to determine how to validate the form as well as determine what fields are required. Because...
21
by: cjl | last post by:
Hey all: I have the following (ugly) code: function preload() { if (cr_series.length >1) preloadCR(); if (ct_series.length >1) preloadCT(); if (mr_series.length >1) preloadMR(); if...
0
by: Steve Jorgensen | last post by:
In a schema I've been working on recently, a convention I evolved was to use a consistent naming pattern such that, for instance, a Resident could appear in a ResidentSet, and could be referred to...
6
by: Ed Jay | last post by:
<disclaimer>New to js.</disclaimer> I have several pages, each with menues comprising checkboxes or radio boxes within the same form. I presently 'brute force' clear the buttons with individual...
13
by: Andrew Bell | last post by:
I'm doing a uni course in the UK and one of my semesters is about programming. This is just going to be compilied and executed with no menu just using command promt (javac classfile.class) I am...
53
by: Hexman | last post by:
Hello All, I'd like your comments on the code below. The sub does exactly what I want it to do but I don't feel that it is solid as all. It seems like I'm using some VB6 code, .Net2003 code,...
46
by: Bruce W. Darby | last post by:
This will be my very first VB.Net application and it's pretty simple. But I've got a snag in my syntax somewhere. Was hoping that someone could point me in the right direction. The history: My...
1
by: janakivenk | last post by:
Hello, I am running Oracle 10g R2 in our office. I created the following procedure. It is suppose to access an xml file ( family.xml). The procedure is compiled and when I try to run it, i get the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.