473,386 Members | 1,883 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,386 software developers and data experts.

Creating custom methods to use with HTML DOM objects

Hi,

I have the following method:

function addClass(el, cl) // passes a DOM node and class name string

What this function does is takes a DOM object as an passed parameter
and applies the passed class. I also have another two:

function removeClass(el, cl) // passes a DOM node and class name
string
function hasClass(el, cl) // checks if a node has a particular class
assigned

addClass, hasClass and removeClass take into consideration that the
DOM object may have more than one class so simply updating/checking
the className property isnt enough, thus the need for a function.

Now .. what I would much prefer to do is call these methods from the
DOM object instead kinda like:

oLink = document.getElementById("home");
oLink.addClass("selected"); // add the 'selected' class to the link
if(oLink.hasClass("selected")) { // check for class 'selected'
oLink.removeClass("selected"); // remove the 'selected' class from
link
}

Is this possible? Obviously I need to pre set this option but I dont
know how to do this. I know how to define something like the
following:

document.getElementsByClassName = function(cl) {...

... but that only applied this custom function to the document node and
not other nodes (makes sense mind you). How do I make this available
to every node? While Ive been writing this, something has come to
mind:

function addClass(cl) {
this.className+= ' '+cl;
}

// apply to every node
oNodes = document.getElementByTagName("*");
for(var i=0; oNodes.length; i++) {
oNodes[i].addClass = addClass;
}
... ive tried this add it appears to work, is this the way this would
be implemented? or, is there a one line way similar to my
getElementsByClassName() example but will apply to ALL relevant nodes
(of nodeType 1 i guess) ? I just feel that if I have a to cycle
through every node on a large page it may hinder things a little
although i may be wrong. Any help? Thanks

Burnsy
Jun 27 '08 #1
1 1611
addClass, hasClass and removeClass take into consideration that the
DOM object may have more than one class so simply updating/checking
the className property isnt enough, thus the need for a function.

Now .. what I would much prefer to do is call these methods from the
DOM object instead kinda like:

oLink = document.getElementById("home");
oLink.addClass("selected"); // add the 'selected' class to the link
if(oLink.hasClass("selected")) { // check for class 'selected'
oLink.removeClass("selected"); // remove the 'selected' class from
link

}

Is this possible?
Yes and no.

You can add functions to a class's prototype using (in this case)

Element.prototype.addClass = addClass

This will work in many browsers, but will not work in IE6,
unfortunately as IE6 does not allow you to alter the Element prototype
in this way.

A little browsing on the internet and I found:
http://www.it-base.ro/2007/07/30/ele...rnet-explorer/

which seems to address your specific question.

For Googling purposes, you best keywords are going to be something
like

Element Prototype Cross-browser

Good luck
Jun 27 '08 #2

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

Similar topics

5
by: rufus | last post by:
Hi, I have a custom collection class that inherits from HashTable. When I add a new CustomObject to my CustomCollection I would like to ensure that it will only accept objects of type...
5
by: Jason Butera | last post by:
I know that I can read/write custom properties of an object by using the following: Setting: document.all.customProp = "this"; Getting: document.all.customProp; Is there a way I can run...
15
by: Carlos Lozano | last post by:
Hi, What is the right way to create an OCX COM component. The component is already registerred, but can't create an instance. I am using the reference to the interop module created. If I use...
3
by: Ken Varn | last post by:
I am just starting the process of creating ASP.NET server controls. I have created controls for .NET applications, but have just started with ASP.NET. I am a little confused about some areas that...
5
by: | last post by:
Trying to learn about manipulating collections of objects, and populating these objects dynamically from datasources. Could someone post a code sample that shows the following: Instantiating a...
0
by: george_Martinho | last post by:
It seems that the ASP.NET Microsoft team didn't think about this!! The profilemanager class has the following methods: - DeleteInactiveProfiles. Enables you to delete all profiles older than a...
0
by: Giorgio | last post by:
It seems that the ASP.NET Microsoft team didn't think about this!! The profilemanager class has the following methods: - DeleteInactiveProfiles. Enables you to delete all profiles older...
26
by: nyathancha | last post by:
Hi, How Do I create an instance of a derived class from an instance of a base class, essentially wrapping up an existing base class with some additional functionality. The reason I need this is...
3
Kelicula
by: Kelicula | last post by:
This is NOT a complete OO perl tutorial However I thought it could be beneficial to explain some of the basic concepts, and allow some users to simplify the software design process. I have...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.