473,387 Members | 1,619 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.

inputing multiple arguments into a function...

question: in the code below, and in general, how would I input multiple
functions into the code, and get multiple outputs? basically, I want to
be able to add say 2 or 3 of the div's to the link so that it shows
multiple div's at once.

<script language="javascript">
<!--
var state = 'none';

function hide(layer_ref) {

if (state == 'block') {
state = 'none';
}
if (document.all) { //IS IE 4 or 5 (or 6 beta)
eval( "document.all." + layer_ref + ".style.display = state");
}
if (document.layers) { //IS NETSCAPE 4 or below
document.layers[layer_ref].display = state;
}
if (document.getElementById &&!document.all) {
hza = document.getElementById(layer_ref);
hza.style.display = state;
}
}
function show(layer_ref) {

if (state == 'none') {
state = 'block';
}
if (document.all) { //IS IE 4 or 5 (or 6 beta)
eval( "document.all." + layer_ref + ".style.display = state");
}
if (document.layers) { //IS NETSCAPE 4 or below
document.layers[layer_ref].display = state;
}
if (document.getElementById &&!document.all) {
hza = document.getElementById(layer_ref);
hza.style.display = state;
}
}
//-->
</script>
<p><a href="#" onclick="hide('div1');">hide me</a></p>
<p><a href="#" onclick="show('div1');">show me</a></p>
<div id="div1" style="display: none;">This is the content 1</div>
<div id="div2" style="display: none;">This is the content 2</div>
<div id="div3" style="display: none;">This is the content 3</div>

Jul 23 '05 #1
1 4269
gr********@gmail.com wrote:
question: in the code below, and in general, how would I input multiple
functions into the code, and get multiple outputs? basically, I want to
be able to add say 2 or 3 of the div's to the link so that it shows
multiple div's at once.

<script language="javascript">
Drop the language attribute, it's been depreciated for years. Use
the required type attribute.

<script type="text/javascript">
<!--
No need to hide scripts anymore, though some still defend the
practice.
var state = 'none';
There is no need for this global variable (see below).

function hide(layer_ref) {

if (state == 'block') {
state = 'none';
}
Here you toggle your global variable. As you need only two states
for your div ('none' and ''), you can just toggle the state in the
one function: if it's currently 'none', make it '' and vice versa.

Unless you really need to toggle between 'block' and 'none', just use
'' and 'none'. '' will return the div to it's default.
if (document.all) { //IS IE 4 or 5 (or 6 beta)
eval( "document.all." + layer_ref + ".style.display = state");
}
if (document.layers) { //IS NETSCAPE 4 or below
document.layers[layer_ref].display = state;
}
if (document.getElementById &&!document.all) {
hza = document.getElementById(layer_ref);
hza.style.display = state;
}
It is good to do feature testing, but why not test for the most
common feature first? Test & use getElementById, then document.all
and finally layers. I suspect that supporting document.layers is no
longer needed, but it's up to you.
}
function show(layer_ref) {
If you just toggle the value of the divs, you don't need this second
function.

[...] }
//-->
No need for hiding any more.
</script>
<p><a href="#" onclick="hide('div1');">hide me</a></p>
the '#' will cause some browsers to scroll back to the top of the
page, which can be annoying, so return false from your onclick.

<p><a href="#" onclick="
hide('div1');
return false;
">hide me</a>
</p>

If you want to pass multiple values to the function:

<p><a href="#" onclick="
hide('div1','div2');
return false;
">hide me</a>
</p>

But then you need some changes in the function (see below)
<p><a href="#" onclick="show('div1');">show me</a></p>
<div id="div1" style="display: none;">This is the content 1</div>
<div id="div2" style="display: none;">This is the content 2</div>
<div id="div3" style="display: none;">This is the content 3</div>


You shouldn't hide things by default, then expose them using
JavaScript, otherwise someone with JS disabled (or a browser that is
incompatible with your JS) will never see the hidden content.

Below is a re-write of your function (I haven't tested the
document.layers stuff). The first one toggles div1, the second takes
as many arguments as you'd like to throw it and it toggles the
elements with the ids passed to it. I dropped support for
document.layers but you could put it back in as per the first
function.

Tested in Firefox and IE.

<script type="text/javascript">
function showHide(layer_ref) {
var hza;
if (document.getElementById) {
hza = document.getElementById(layer_ref);
} else if (document.all) {
hza = document.all['layer_ref'];
}
if (hza && hza.style) {
if (hza.style.display == '') {
hza.style.display = 'none';
return 'Show ' + layer_ref;
} else {
hza.style.display = '';
return 'Hide ' + layer_ref;
}
} else if (document.layers) {
hza = document.layers[layer_ref];
if (hza.display == '') {
hza.display = 'none';
return 'Show ' + layer_ref;
} else {
hza.style.display = '';
return 'Hide ' + layer_ref;
}
}
}

function showHide2() {
var i = arguments.length;
var hza;
while (i--){
if (document.getElementById) {
hza = document.getElementById(arguments[i]);
} else if (document.all) {
hza = document.all[arguments[i]];
}
if (hza && hza.style) {
hza.style.display = (hza.style.display == '')? 'none':'';
}
}
}

</script>
<p><a href="#" onclick="
this.innerHTML = showHide('div1');
return false;
">Hide div1</a></p>
<p><a href="#" onclick="
showHide2('div2','div3');
return false;
">Show/hide div2 & div3</a></p>

<div id="div1">This is the content 1</div>
<div id="div2">This is the content 2</div>
<div id="div3">This is the content 3</div>

--
Rob
Jul 23 '05 #2

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

Similar topics

66
by: Darren Dale | last post by:
Hello, def test(data): i = ? This is the line I have trouble with if i==1: return data else: return data a,b,c,d = test()
17
by: Roland Hall | last post by:
Is there a way to return multiple values from a function without using an array? Would a dictionary object work better? -- Roland Hall /* This information is distributed in the hope that it...
20
by: km | last post by:
Hi all, In the following code why am i not able to access class A's object attribute - 'a' ? I wishto extent class D with all the attributes of its base classes. how do i do that ? thanks in...
5
by: deko | last post by:
I'd like to use a bit of code in the OnOpen event of a report: =rptOpen(Me.ReportName), (Me.Tag) --this doesn't work This does work: Private Sub Report_Open(Cancel As Integer)...
1
by: glennpierce | last post by:
Hi I was wondering if anyone knows of a method to achieve the creation of events in c. currently I use a function pointer to call one callback. However, I really need to map the function pointer or...
47
by: Mark | last post by:
why doesn't .NET support multiple inheritance? I think it's so silly! Cheers, Mark
8
by: aleksandar.ristovski | last post by:
Hello all, I have been thinking about a possible extension to C/C++ syntax. The current syntax allows declaring a function that returns a value: int foo(); however, if I were to return...
7
by: andrewfsears | last post by:
I have a question: I was wondering if it is possible to simulate the multiple constructors, like in Java (yes, I know that the languages are completely different)? Let's say that I have a class...
1
by: ashokbio | last post by:
How to return multiple values by passing multiple arguments in function or subroutine using VB6? T. Ashok Kumar
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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.