There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
 
Web Design & Development
Tag Cloud
adware audio bios blue screen boot bsod computer crash dell desktop driver drivers email error excel firefox freeze google hard drive hardware hijackthis install internet laptop linux malware network no sound outlook problem router screen server slow sound speakers spyware startup trojan usb video virus vista vundo webcam windows windows 7 windows vista windows xp wireless
Search
Search for:
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
Javascript Help

Tip: Click here to scan for System Errors and Optimize PC performance
[ Sponsored Link ]

Closed Thread
 
Thread Tools
FallFromINFINITY's Avatar
Computer Specs
Member with 71 posts.
 
Join Date: May 2008
Location: Western NY
Experience: Intermediate
01-Jul-2009, 04:07 PM #1
Question Javascript Help
A simple hide on click code.
Code:
function dispHandle(obj) {
if (obj.style.display == 'none') {
obj.style.display = '';
} else {
obj.style.display = 'none';
} }
Code:
<p id="hide">Hello</p>
<a onclick="dispHandle(hide)">Hide/show text</a>
Here's what I need help with. This code only works if the object you want to control has an ID attribute. I need it to work for something that only has a CLASS attribute. The main reason is that I need a button to control a dozen different parts, simultaneously. Finding the file for each one of the parts and editing the code to add a ID attribute would take a lot of time, and would be very easy to miss several of the objects. There are over 43 files that would need this done to it. But they all already have the same CLASS attribute, so it would be easier to modify this one instance of code that it would be to modify 40+.
__________________
Who is General Error, and why is he reading my harddisk?
MMJ's Avatar
MMJ MMJ is offline
Distinguished Member with 3,505 posts.
 
Join Date: Oct 2006
05-Jul-2009, 11:51 PM #2
In the older version's of browsers you can't get elements by class.

Your best bet would be to loop through all p elements, check their class to find the one that needs to be hidden, then set a variable to that element.

Then in the function look for that variable and toggle its visibility.
__________________
higher level web dev:
PHP | JS
FallFromINFINITY's Avatar
Computer Specs
Member with 71 posts.
 
Join Date: May 2008
Location: Western NY
Experience: Intermediate
06-Jul-2009, 12:34 AM #3
Quote:
Originally Posted by MMJ View Post
In the older version's of browsers you can't get elements by class.

Your best bet would be to loop through all p elements, check their class to find the one that needs to be hidden, then set a variable to that element.

Then in the function look for that variable and toggle its visibility.
Here's the problem: The webpages are in php, and the elements are part of/are dozens of separate files. The elements have different tags, ranging from div to table to and several others. The page has several variables, and loads different parts of itself, causing different elements to added to the page. I am looking for a way to select all of the elements, when the only thing that is similar is the class. It needs to be done in a way that does not reload the page, and can be undone, without reloading the page.
__________________
Who is General Error, and why is he reading my harddisk?
MMJ's Avatar
MMJ MMJ is offline
Distinguished Member with 3,505 posts.
 
Join Date: Oct 2006
06-Jul-2009, 10:19 AM #4
Either loop through all the elements looking for that class or do something like this:

http://www.quirksmode.org/blog/archi...mentsbycl.html

i.e. use document.getElementsByClassName when the browser supports it and when it doesn't use a custom function.
FallFromINFINITY's Avatar
Computer Specs
Member with 71 posts.
 
Join Date: May 2008
Location: Western NY
Experience: Intermediate
06-Jul-2009, 03:20 PM #5
How would integrate that into the code that I already have? I'm newer to javascript, so I'm not entirely sure exactly how to run this.

If it helps, the class of the elements is "border" and the hide/show script is to be run on a button click. Really, all I need is: when the button is clicked, to change the visibility from "visible" to "none" and back when it's clicked again.
__________________
Who is General Error, and why is he reading my harddisk?
MMJ's Avatar
MMJ MMJ is offline
Distinguished Member with 3,505 posts.
 
Join Date: Oct 2006
10-Jul-2009, 04:44 PM #6
basic code:
Code:
<a href="#" onclick="toggleEl();">Toggle</a>

function toggle(){
	var i;
	i = i ? 0 : 1;
	document.getElementsByClassName('border')[0].visibility = i ? "none" : "visible";
}
FallFromINFINITY's Avatar
Computer Specs
Member with 71 posts.
 
Join Date: May 2008
Location: Western NY
Experience: Intermediate
11-Jul-2009, 01:09 AM #7
perfect. The site is down at the moment. So I'll get back to you if it works or not as soon as it's online again.
Closed Thread Bookmark and Share

Tags
class attribute, javascript

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Smart Search

Find your solution!



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.

Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 12:42 PM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.
Powered by Cermak Technologies, Inc.