There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Web Design & Development
Tag Cloud
access acer asus bios bsod computer crash drive driver drivers error ethernet excel freeze games gaming hard drive hardware hdmi internet java laptop malware memory monitor motherboard music network obp printer problem ram random registry router slow software sound trojan usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
Javascript Help

Reply  
Thread Tools
FallFromINFINITY's Avatar
Computer Specs
Member with 100 posts.
 
Join Date: May 2008
Location: Western NY
Experience: Intermediate
01-Jul-2009, 05: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
Senior Member with 3,637 posts.
 
Join Date: Oct 2006
06-Jul-2009, 12:51 AM #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.
FallFromINFINITY's Avatar
Computer Specs
Member with 100 posts.
 
Join Date: May 2008
Location: Western NY
Experience: Intermediate
06-Jul-2009, 01: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
Senior Member with 3,637 posts.
 
Join Date: Oct 2006
06-Jul-2009, 11: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 100 posts.
 
Join Date: May 2008
Location: Western NY
Experience: Intermediate
06-Jul-2009, 04: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
Senior Member with 3,637 posts.
 
Join Date: Oct 2006
10-Jul-2009, 05: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 100 posts.
 
Join Date: May 2008
Location: Western NY
Experience: Intermediate
11-Jul-2009, 02: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.
Reply

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.

Search Tech Support Guy

Find the solution to your
computer problem!




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



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 07:43 AM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.