Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-02-2009, 08:09 PM   PM User | #1
Xibe
New Coder

 
Join Date: Nov 2005
Posts: 66
Thanks: 8
Thanked 0 Times in 0 Posts
Xibe is an unknown quantity at this point
Question Javascript "Classes"

I'm a noob when it comes to javascript. Excuse my ignorance!

Is there any way to replicate the CSS version of "class" for javascript?

Say I have like seventeen elements that I want to all use the same javascript:
Code:
onmouseover="this.style.backgroundColor='#e6e6e6'; this.style.cursor='pointer';" onmouseout="this.style.backgroundColor='#FFFFFF';" onclick="toggle('type'); toggle('mail')
It would be a pain to copy all of those into each div/table/td etc. Isn't there a way to replicate this kind of functionality: class="toggle" to make it easier?

I searched some ways on Google and found some good ways of doing stuff through functions... but not really what I was looking for.

Last edited by Xibe; 02-02-2009 at 08:16 PM..
Xibe is offline   Reply With Quote
Old 02-02-2009, 11:12 PM   PM User | #2
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,455
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
don't copy repetive stuff into html, it usually just adds messiness and bloat.


you can use a class or name to group the elements, or just make a list of their id's, or collect each tag under a common parent.

once you have a group of tags selected, upgrade them in a loop when the page loads.

you the onload event as a chance to cycle through the tags, adding style, classes, and events to each tag.

the html in your document doesn't need to change to expand the functionality; you only have to modify the group of tags you modify onload.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 02-02-2009, 11:58 PM   PM User | #3
Xibe
New Coder

 
Join Date: Nov 2005
Posts: 66
Thanks: 8
Thanked 0 Times in 0 Posts
Xibe is an unknown quantity at this point
I'm sorry... I don't understand what you're saying I should do.

Quote:
upgrade them in a loop when the page loads
How do I do that? What does it mean to upgrade them in a loop?

Quote:
you the onload event as a chance to cycle through the tags, adding style, classes, and events to each tag.
What do you mean by this?
Xibe is offline   Reply With Quote
Old 02-03-2009, 06:17 AM   PM User | #4
gun5
New Coder

 
Join Date: Feb 2009
Location: Jakarta, Indonesia
Posts: 93
Thanks: 2
Thanked 20 Times in 20 Posts
gun5 is an unknown quantity at this point
Hi,

I'm a newbie too. Is this help?

Code:
<html>
<head>
<script language="javascript">
function fnMover(){
this.style.background="#06f";
}
function fnMout(){
this.style.background="#fff";
}
function cls(sObj,sCls,sE1,sE2){
var o = document.getElementsByTagName(sObj);
	for(j=0;j<o.length;j++){
		if(o[j].className==sCls){
			// netscape
			o[j].addEventListener(sE1,fnMover,false);
			o[j].addEventListener(sE2,fnMout,false);
		}
	}
}

window.onload=function(){
cls("p","prg","mouseover","mouseout");
}
</script>
</head>
<body>
<p class="prg">par 1</p>
<p class="prg">par 2</p>
</body>
</html>
gun5 is offline   Reply With Quote
Reply

Bookmarks

Tags
class, css, duplicate, html, javascript

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:09 PM.


Advertisement
Log in to turn off these ads.