Does anybody know a simple way to toggle between buttons.
The functions I have used: -
function setClass(objectID,newClass){
-
var dom = findDOM(objectID,0);
-
dom.className = newClass;
-
}
-
-
function setVisibility(objectID,state){
-
var dom = findDOM(objectID,1);
-
dom.visibility = state;
-
}
-
Plus this monster to build a cross browser dom: -
var isDHTML = 0;
-
var isLayers = 0;
-
var isAll = 0;
-
var isID = 0;
-
-
if (document.getElementById) {isID = 1; isDHTML = 1;}
-
else {
-
if (document.all) {isAll = 1; isDHTML = 1;}
-
else {
-
browserVersion = parseInt(navigator.appVersion);
-
if ((navigator.appName.indexOf('Netscape') != -1) && (browserVersion == 4)) {isLayers = 1; isDHTML = 1;}
-
}}
-
-
function findDOM(objectID,withStyle) {
-
if (withStyle == 1) {
-
if (isID) { return (document.getElementById(objectID).style) ; }
-
else {
-
if (isAll) { return (document.all[objectID].style); }
-
else {
-
if (isLayers) { return (document.layers[objectID]); }
-
};}
-
}
-
else {
-
if (isID) { return (document.getElementById(objectID)) ; }
-
else {
-
if (isAll) { return (document.all[objectID]); }
-
else {
-
if (isLayers) { return (document.layers[objectID]); }
-
};}
-
}
-
}
-
-
The code I have produced - an insult to every decent programmer (there must be an easier way): -
<td> <a href="javascript:void('')" class="navi" id="insp" onMouseUp="setClass('insp','naviNA');setClass('berat','navi');setClass('plan','navi');setClass('real','navi');setVisibility('inspText','visible');setVisibility('beratText','hidden');setVisibility('planText','hidden');setVisibility('realText','hidden');textIsVisible='yes'"><b>Inspirieren</b></a></td>
-
<td><img src="img/Orange1x1.gif" width="2" height="22"></td>
-
<td><a href="javascript:void('')" class="navi" id="berat" onMouseUp="setClass('insp','navi');setClass('berat','naviNA');setClass('plan','navi');setClass('real','navi');setVisibility('inspText','hidden');setVisibility('beratText','visible');setVisibility('planText','hidden');setVisibility('realText','hidden');textIsVisible='yes'"><b>Beraten</b></a></td>
-
<td><img src="img/Orange1x1.gif" width="2" height="22"></td>
-
<td><a href="javascript:void('')" class="navi" id="plan" onMouseUp="setClass('insp','navi');setClass('berat','navi');setClass('plan','naviNA');setClass('real','navi');setVisibility('inspText','hidden');setVisibility('beratText','hidden');setVisibility('planText','visible');setVisibility('realText','hidden');textIsVisible='yes'"><b>Planen</b></a></td>
-
<td><img src="img/Orange1x1.gif" width="2" height="22"></td>
-
<td><a href="javascript:void('')" class="navi" id="real" onMouseUp="setClass('insp','navi');setClass('berat','navi');setClass('plan','navi');setClass('real','naviNA');setVisibility('inspText','hidden');setVisibility('beratText','hidden');setVisibility('planText','hidden');setVisibility('realText','visible');textIsVisible='yes'"><b>Realisieren</b></a></td>
-
13 1707
You only need document.getElementById(), the standard method, unless you want to support IE4 and Netscape 4, two old buggy browsers that are over 10 years old. All modern browsers support document.getElementById().
An easier way to toggle buttons is to have one function which takes the button name/id as an argument and hides the rest or changes their class except the one to be shown/set.
Use document.getElementsByTagName(tagName) to get all the tags (where tagName is the name of the tag). Are the buttons input (type=button) tags? What about the text?
Thanks for getting back to me.
You only need document.getElementById(), the standard method, unless you want to support IE4 and Netscape 4, two old buggy browsers that are over 10 years old. All modern browsers support document.getElementById().
Ah, yes. Good point. Guess I should have read a newer book when trying to teach myself. ;-\
An easier way to toggle buttons is to have one function which takes the button name/id as an argument and hides the rest or changes their class except the one to be shown/set.
Use document.getElementsByTagName(tagName) to get all the tags (where tagName is the name of the tag). Are the buttons input (type=button) tags? What about the text.
The buttons are just text links. I don't want to change the class of all the <a> tags in that document only of certain IDs. Perhaps building an array with the IDs that I would want to set to a different class would be a start. Flagging the clicked button with a variable and then letting that array run setting a new class for all the button layers but the flagged one.
To be honest I haven't worked it out yet. Part of the problem is to figure Javascript out. I have not used it before.
The buttons are just text links. I don't want to change the class of all the <a> tags in that document only of certain IDs. Perhaps building an array with the IDs that I would want to set to a different class would be a start. Flagging the clicked button with a variable and then letting that array run setting a new class for all the button layers but the flagged one.
That's a possibility or by using elem.getElementsByTagName(tagName) where elem is the parent element containing the links required only and no other links, e.g. the table row.
That's a possibility or by using elem.getElementsByTagName(tagName) where elem is the parent element containing the links required only and no other links, e.g. the table row.
Ah, that is confusing me. I guess "elem" is another object like - document.elem.getElementeByTagName(tagName).
which I woul need to address: - document.getElementByID(elem).getElementeByTagName(tagName).
Of course not, but how is it posssible for the browser to find that particular element (in my case a table).
Does the following code make any sense to you? -
buttonIDs = new Array("'insp','berat','plan','real'")
-
numberButtons = buttonIDs.length
-
ButtonCounter = 0
-
function toggleButtons() {
-
ButtonCounter++
-
if (buttonClicked == 'yes')
-
document.getElementById(buttonIDs).className='naviNA';
-
buttonClicked = 'no';
-
else {
-
document.getElementById(buttonIDs).className='navi';
-
buttonClicked = 'no';
-
}
-
and: -
<td><a href="javascript:void('')" class="navi" id="licht" onMouseUp="buttonClicked='yes';toggleButtons(); ImgKap='l';document.sqImg.src= eval(ImgKap + '01.src')"><b>Licht</b></a></td>
It doesn't work, but unfortunately I have to rush off to work now.
how is it posssible for the browser to find that particular element (in my case a table).
'elem' was just a reference to the parent element containing the links. You would obviously have to reference the element. The advantage of this approach is that if you add another link, you don't need to change the rest of the code.
Does the following code make any sense to you?
Use something like: - var buttonIDs = new Array('insp','berat','plan','real');
-
function toggleButtons(button) {
-
var id = button.id;
-
for (i = 0; i < buttonIDs.length; i++) {
-
if (buttonIDs[i] == id) {
-
document.getElementById(buttonIDs[i]).className='naviNA';
-
} else {
-
document.getElementById(buttonIDs[i]).className='navi';
-
}
-
}
-
}
and: - <a href="..." class="navi" id="licht" onmouseup="toggleButtons(this);">
'elem' was just a reference to the parent element containing the links. You would obviously have to reference the element. The advantage of this approach is that if you add another link, you don't need to change the rest of the code.
Hm, it is slowly sinking in.
Use something like: - var buttonIDs = new Array('insp','berat','plan','real');
-
function toggleButtons(button) {
-
var id = button.id;
-
for (i = 0; i < buttonIDs.length; i++) {
-
if (buttonIDs[i] == id) {
-
document.getElementById(buttonIDs[i]).className='naviNA';
-
} else {
-
document.getElementById(buttonIDs[i]).className='navi';
-
}
-
}
-
}
and: - <a href="..." class="navi" id="licht" onmouseup="toggleButtons(this);">
Slick. I can see that you are acoder and I am not. ;-)
Anyway some of my buttons make text visible and invisible. I think you were asking about it above. The textID is like the buttonID plus the word "text".
I thought perhaps one could write something like: -
-
var textButtonIDs = new Array('insp','berat','plan','real');
-
function changeTextButtons(textButton) {
-
var id = textButton.id;
-
for (i = 0; i < textButtonIDs.length; i++) {
-
if (textButtonIDs[i] == id) {
-
document.getElementById(textButtonIDs[i]).className='naviNA';
-
-
var ButtonText = eval(textButtonIDs[i] + Text);
-
document.getElementById(ButtonText).visibility = 'visible';
-
-
-
} else {
-
document.getElementById(textButtonIDs[i]).className='navi';
-
document.getElementById(ButtonText).visibility = 'hidden';
-
}
-
}
-
}
-
It doesn't work and my problem is I have not really understood how to use variables in functions.
I did write a second version of my first attempt and posted it as a reply to myself, but the system did not take it.
Why is your code in colour and mine black and white?
Try this: - var textButtonIDs = new Array('insp','berat','plan','real');
-
function changeTextButtons(textButton) {
-
var id = textButton.id;
-
for (i = 0; i < textButtonIDs.length; i++) {
-
var ButtonText = textButtonIDs[i] + "Text";
-
if (textButtonIDs[i] == id) {
-
document.getElementById(textButtonIDs[i]).className='naviNA';
-
document.getElementById(ButtonText).visibility = 'visible';
-
} else {
-
document.getElementById(textButtonIDs[i]).className='navi';
-
document.getElementById(ButtonText).visibility = 'hidden';
-
}
-
}
-
}
Note the changes. No need for eval. It's just a string that you pass to getElementById(). I've moved the declaration out of the if statement because it would not be available in the else part otherwise.
Why is your code in colour and mine black and white?
Add the language to the code tag, e.g. [code=javascript], [code=html]
I see. Excellent. Thank you!
I have got a similar situation with another set of buttons. Only now if you click on the button it calls a function that changes an image according to the chapter you are in.
I wrote the following function: -
-
function chgCompImg(imgField,newImg,ImgNr) {document[imgField].src = eval(newImg + ImgNr + ".src")}
-
-
It is called like this: -
<a href="javascript:void('')" class="navi" id="stoffe" onMouseUp="toggleButtons(this);ImgKap='s';chgCompImg('sqImg',ImgKap,'01')">
-
Well, it is probably not possible to call that function from within a function like so: -
var buttonIDs = new Array('atHome','kitchen','checkl','rezept','moebel','licht','stoffe','real','kontakt','anfahrt','impressum');
-
function toggleButtons(button) {
-
var id = button.id;
-
for (i = 0; i < buttonIDs.length; i++) {
-
if (buttonIDs[i] == id) {
-
document.getElementById(buttonIDs[i]).className='naviNA';
-
chgCompImg('sqImg',ImgKap,'01');
-
} else {
-
document.getElementById(buttonIDs[i]).className='navi';
-
}
-
}
-
}
-
Or is it better just to write? -
document.sqImg.src = eval(ImgKap+ "01" + ".src");
-
I have got a similar situation with another set of buttons. Only now if you click on the button it calls a function that changes an image according to the chapter you are in.
What does the img tag look like? Is it named "sqImg"? Note that there's no need to use eval since all you need is a string.
What does the img tag look like? Is it named "sqImg"? Note that there's no need to use eval since all you need is a string.
Yes that's right "sqImg" is the image tag.
Okay, "eval" is "evil" so I have read.
Would -
document.sqImg.src = ImgKap+ "01.src";
-
be the right thing to write into the function above?
-
document.images[imgField].src = document.images[newImg + ImgNr].src;
-
should be fine.
-
document.images[imgField].src = document.images[newImg + ImgNr].src;
-
should be fine.
Well thank you, acoder!
I am going to abandon this thread now. There is really not much more to ask regarding that subject (for now). And it's getting "too much scrolling" anyway.
But your input was invaluable. I feel I learned an awful lot (beginners have a steep learning curve).
;-) youssou
No problem, you're welcome. Glad I could help as part of the learning process.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Alan Clark |
last post by:
Dear All
I need to do something very simple with Javascript and have been looking
all over the web for two days for a suitable script. I'm the kind of person
who learns by seeing how it's done....
|
by: Richard |
last post by:
The style sheet shown below is from the suckerfish vertical menu.
http://www.htmldog.com/articles/suckerfish/dropdowns/example/vertical.html
I've added in a few minor changes to color code the...
|
by: Simon Wigzell |
last post by:
Is it possible within javascript to stop the little pulldown menu (with the
values that the browser remembers have previously been entered into the
field) from appearing? I have one very dense form...
|
by: DaveO |
last post by:
Hi All
I have no experience in JavaScript ( only html and ASP ).
I would like to add a Menu ( with multi level drop down sub menu's ) to my
web site.
I have tried using a DHTML Menu...
|
by: Digital Puer |
last post by:
I have the following HTML form:
- radio button A (default selected)
- radio button B
- input field, of type "file" with "Choose" button
- submit button
I would like to have it so that if the...
|
by: Trint Smith |
last post by:
Ok,
On my website, tribidz.com, there is one page that needs to not have any
buttons or menu or address bar. How can I do this? What is the html
code or whatever?
Thanks,
Trint
..Net...
|
by: www.web20developers.com |
last post by:
http://www.web20developers.com
http://www.web20developers.com/index.php?option=com_content&task=view...
Ajallerix : AJAX, simple, fast Web image gallery demo ; at Novell
AJAX -...
|
by: tim |
last post by:
I am using foldoutmenu 3 and am having problems with viewing my menus
in firefox. On my sub3 menus i have more than one line of text in some
places. firefox does not recognise that there is more...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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,...
|
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...
|
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,...
| | |