473,804 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to improve this script?

Is there a simpler, more elegant way to specify what is shown and what is
hidden than the following primitive script?

<script type="text/javascript">
var pN = new Array();
pN["1"]="Text 1.";
pN["2"]="Text 2";
pN["3"]="Text 3";
pN["4"]=""Text 4";
pN["5"]="Text 5";
pN["6"]="Text 6";

function showPHelp(n) {
document.getEle mentById('pSpot ').innerHTML = pN[n];
show('test1');
//The following is what I want to improve on
if (n==1) {show('image1') ;hide('image2') ;}
else if (n==2) {hide('image1') ;show('image2') ;}
..
..
else if (n==6)
{show('image6') ;hide('image1') ;hide('image2') ;......;hide('i mage5');}
}

<body>
<div id="test1" style="display: none;"><span id="pSpot"></span>

<div id="image1" style="display: none;"><img src='p1.jpg'... ></div>
<div id="image2" style="display: none;"><img src='p2.jpg'... ></div>
...
...
<div id="image62" style="display: none;"><img src='p6.jpg'... ></div>

<a name="" onclick="hide(' test1');">Close </a>
</div>

<br><br>
<a name="" onclick="showPH elp(1);">?1</a>
<br><br>
<a name="" onclick="showPH elp(2);">?2</a>
...
...
<a name="" onclick="showPH elp(6);">?6</a>

Thank you,
Herb
Jul 29 '06 #1
2 1281
He***@X.net said the following on 7/28/2006 10:47 PM:
Is there a simpler, more elegant way to specify what is shown and what is
hidden than the following primitive script?
Yes, use a case/switch block. Or, use dynamic variable names.
<script type="text/javascript">
var pN = new Array();
pN["1"]="Text 1.";
pN["2"]="Text 2";
pN["3"]="Text 3";
pN["4"]=""Text 4";
pN["5"]="Text 5";
pN["6"]="Text 6";

function showPHelp(n) {
document.getEle mentById('pSpot ').innerHTML = pN[n];
show('test1');
//The following is what I want to improve on
if (n==1) {show('image1') ;hide('image2') ;}
else if (n==2) {hide('image1') ;show('image2') ;}
switch (n){
case 1:
hide('image2');
show('image1');
break;
case 2:
hide('image1');
show('image2');
break;

And so on. But I am not even sure your problem is as hard as you make it
out to be.

If all showPHelp does is set the innerHTML of a div tag, show an image
and hide an image then it can all be a lot simpler.

Rather than hide a div tag and show another to change images, simply
change the img tags .src property and change the image:

document.images['theImage'].src = newImage;

And then change your innerHTML.

function doIt(n){
document.getEle mentById('pSpot ').innerHTML = pN[n][0];
document.images['theImage'].src = pN[n][1];
}

And change your array definition to something like this:

pN["1"]= new Array("Text 1.","image src for that text");

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Jul 29 '06 #2
Randy Webb scribed:
>He***@X.net said the following on 7/28/2006 10:47 PM:
>Is there a simpler, more elegant way to specify what is shown and what is
hidden than the following primitive script?

Yes, use a case/switch block. Or, use dynamic variable names.
><script type="text/javascript">
var pN = new Array();
pN["1"]="Text 1.";
pN["2"]="Text 2";
pN["3"]="Text 3";
pN["4"]=""Text 4";
pN["5"]="Text 5";
pN["6"]="Text 6";

function showPHelp(n) {
document.getEle mentById('pSpot ').innerHTML = pN[n];
show('test1');
//The following is what I want to improve on
if (n==1) {show('image1') ;hide('image2') ;}
else if (n==2) {hide('image1') ;show('image2') ;}

switch (n){
case 1:
hide('image2');
show('image1');
break;
case 2:
hide('image1');
show('image2');
break;

And so on. But I am not even sure your problem is as hard as you make it
out to be.

If all showPHelp does is set the innerHTML of a div tag, show an image
and hide an image then it can all be a lot simpler.

Rather than hide a div tag and show another to change images, simply
change the img tags .src property and change the image:

document.image s['theImage'].src = newImage;

And then change your innerHTML.

function doIt(n){
document.getEl ementById('pSpo t').innerHTML = pN[n][0];
document.image s['theImage'].src = pN[n][1];
}

And change your array definition to something like this:

pN["1"]= new Array("Text 1.","image src for that text");
Excellent! Thank you.

Herb
Jul 29 '06 #3

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

Similar topics

0
1541
by: Allyson Griffin | last post by:
--__A455_55B08..702A Content-Type: text/html; Content-Transfer-Encoding: quoted-printable <html>helloooo Men improve your LUV life with these nohc <br> <body> <br>Quality purr^scriptions for adults that require REAL medications for a variety of needs and ailments. We have hundreds of popular names to choose from.
0
1568
by: TonyJeffs | last post by:
The following gimmicky code shows the icons of the recipients desktop on a webpage, provided hes using ie, and c:/windows/desktop. The trouble is ,it shows them in a column down the left of the window. Can I add something to it to make it Autoarrange the icons? Thanx Tony
4
2062
by: Jari Aalto | last post by:
Please suggest comments how can I make this script to work from bash. Also how can I skip better the argument from command line without hte extra variable i? #!/bin/bash function compile () { python -c ' import os, sys, py_compile;
9
4620
by: Peng Jian | last post by:
I have a function that is called very very often. Can I improve its efficiency by declaring its local variables to be static?
5
1541
by: Cenk Genisol | last post by:
Hi NG, I am relatively new to Web developement and have some questions to you. I've got a Masterpage where some graphics are embedded by CSS. They format the borders of tables, which store page content so it's just an optical background. During the process of loading the page on the client, pages which implement the above mentioned masterpage are build very excursive. That looks
11
3390
by: Peted | last post by:
Im using c# 2005 express edition Ive pretty much finished an winforms application and i need to significantly improve the visual appeal of the interface. Im totaly stuck on this and cant seem to work out how to start on a solution. I have of course used a varienty of componets, mostly radio buttons with "button" appearence.
6
2317
by: blackice | last post by:
hello all ..., first i would thank all the people who are in this forums cause they are very helpful ..., By the way i had some posts hers about comment out the named.conf file the script is simply comment out the zones in the file and it works well while i was testing scrpt i found that the script is working in an fixed way which means that the zone must be 5 lines if it not a five line the script won't function as needed ., Here...
2
2780
by: sdanda | last post by:
Hi , Do you have any idea how to improve my java class performance while selecting and inserting data into DB using JDBC Connectivity ......... This has to work for more than 8,00,000 of records ..... Can you give some performance tips if you have known 1) For this I am using oci driver ( because I m using oracle 10g) instead of thin driver 2) In that programme I m using prepared statement instead of statement 3) I am...
4
1837
by: DG | last post by:
Alright, I have searched and searched and read many conversations on the topic of relative and absolute imports and am still not getting the whole thing through my skull. Highlights of what I've read: http://mail.python.org/pipermail/python-list/2007-January/422973.html http://groups.google.com/group/comp.lang.python/browse_thread/thread/b1e8dc93471a7079/8751c82cfe1ca3f2?lnk=gst&q=absolute+import#8751c82cfe1ca3f2...
0
9711
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9593
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10595
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10335
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10088
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5668
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4306
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.