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 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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?
|
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#):
|
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...
|
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...
| |
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
|
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
|
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
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |