473,803 Members | 3,095 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.getEle mentById("home" );
oLink.addClass( "selected") ; // add the 'selected' class to the link
if(oLink.hasCla ss("selected") ) { // check for class 'selected'
oLink.removeCla ss("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.getEle mentsByClassNam e = 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.getEle mentByTagName(" *");
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
getElementsByCl assName() 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 1629
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.getEle mentById("home" );
oLink.addClass( "selected") ; // add the 'selected' class to the link
if(oLink.hasCla ss("selected") ) { // check for class 'selected'
oLink.removeCla ss("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.prototy pe.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
2192
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 CustomObject. How would I do this? Thanks in advance. Public Class CustomCollection Inherits Hashtable ' default constructor
5
2216
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 code when this custom property is set. Or perhaps there is a way to create a custom method?
15
6752
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 javascript or vbscript it works. I will appreciate any help. I do the following (C#):
3
1876
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 I am hoping someone can help clear up. 1. What is the difference between initializing a control in the constructor, vs the OnInit(), vs the CreateChildControls() methods? 2. The control that I created contains an Items collection attribute...
5
2291
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 collection object -- say, a dictionary. Populating that collection object with custom objects, say, Person. What I really want to see is how to populate the properties of those Person objects from a datasource: instantiate one Person, fill...
0
1685
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 specified date. - DeleteProfile. Enables you to delete a profile associated with a specified username. - DeleteProfiles. Enables you to delete a set of profiles. - FindInactiveProfilesByUserName. Returns a collection of ProfileInfo
0
2578
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 than a specified date. - DeleteProfile. Enables you to delete a profile associated with a
26
5378
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 because I am not always able to control/create all the different constructors the base class has. My problem can be described in code as follows ... /* This is the base class with a whole heap of constructors/functionality*/ public class Animal
3
7430
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 found it to be extremely useful and beneficial to create my own "custom" modules. It (among many other things) allows you to type less. Or to make your keystrokes matter more, however you look at it. (If you do even look at it.) Things the reader...
0
9703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9565
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10069
tracyyun
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9125
agi2029
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5501
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5633
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2972
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.