Register  |  Login




Advertisement

Start Your Own Q&A Site

Create your own Q&A site easily, allowing you to quickly grow a new community around any subject matter or generate new organic traffic for your existing website.

Question

Status: Closed Points: 75 Time: 11:37 - Nov 26, 2006  

jgivoni

How do I add methods to DOM element objects in javascript?

I am a bit confused by this - I have been trying to add a method that will return true or false whether an element (retrieved with document.getElementById("example")) has a specified class name assigned.
I have tried with the "prototype" method and without - I have used "= new Function(string)" and "= function(){}" syntax and sometimes it works in one browser, sometimes in another, but I can't figure out why or why not.

Please shed some light on this for me; this is what I want to do:

x = document.getElementById("example");
if (x.hasClassName("test"))
{
<something>
}

(and I am not interested in an alternative way to do it...)

Answer Discussion
Tutorials

 

xarcus

Date:: Nov 28, 2006

Time:: 12:40

With Firefox / Mozilla it works as expected

HTMLElement.prototype.hasClassName = function( test){ return this.className == test}

With MSIE 6.0 document.getElementById( "myID").property shows "undefined"

MSoft does not follow standards. There is also a compile switch that affects prototype:

( See Note at http://msdn2.microsoft.com/en-US/library...)

>> The prototype property of a built-in object cannot be modified when running in fast mode, the default for JScript. To compile a program from the command line that uses the prototype property, you must turn off the fast option by using /fast-. It is not safe to turn off the fast option in ASP.NET because of threading issues.

xarcus

Date:: Nov 28, 2006

Time:: 12:44

I am sorry! fortget previous msg. I wrote property instead of prototype at With MSIE 6.0 document.getElementById( "myID").pro...

With Firefox / Mozilla it works as expected

HTMLElement.prototype.hasClassName = function( test){ return this.className == test}

With MSIE 6.0 document.getElementById( "myID").prototype shows "undefined"

MSoft does not follow standards. There is also a compile switch that affects prototype:

( See Note at http://msdn2.microsoft.com/en-US/library.......)

>> The prototype property of a built-in object cannot be modified when running in fast mode, the default for JScript. To compile a program from the command line that uses the prototype property, you must turn off the fast option by using /fast-. It is not safe to turn off the fast option in ASP.NET because of threading issues.

xarcus

Date:: Nov 28, 2006

Time:: 12:59

Check this link too: http://www.codeproject.com/jscript/cross...
at the paragraph "Prototypes with HTML objects"

xarcus

Date:: Nov 28, 2006

Time:: 13:27

So you may add methods to HTL objects only in non MSIE browsers

if (isGecko) { // add the method
HTMLElement.prototype.hasClassName = function( test){ return this.className == test}
}
else if (isMSIE) { // add the function
var hasClassName = function( elem, test){ return elem.className == test}
}

jgivoni

Date:: Jan 09, 2007

Time:: 15:01

Ok, xarcus, thanks for that.

What I understand now is that IE doesn't 'expose' the html-element object so that it is impossible to 'prototype' it.

I've tried some of the scripts that are supposed to define the 'missing' HTMLElement in IE and copy all the properties from the 'unidentified' object but for some reason I didn't get it to work.

Everything works wonderfully well in most other browsers though.

Jakob

Question Answered

This question has been closed, and points have been rewarded to the following experts:


xarcus: 75

You're welcome however to comment or give additional information or if you wish, you have the ability to write a Tutorial in the Tutorial Area.

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

No tutorials have been submitted yet. Want to be the first?

Answer this Question

New User

Email:

Upon submission of this form, you will automatically be registered as a Quomon user and we will send your login information to this address

Registered User

Username:

Password:

Forgot Your Password?

Ask a Question

Have a new question? Ask!

You have 100 characters to use



Top Experts

View More

Rank

Expert

Points

1.

nidhi

10354

2.

oracleofDelphi

6493

3.

rcastagna

5596

4.

LAGM

4848

5.

PeterNZ

3487

6.

gonzalo

2840

7.

Mason

2770

8.

jgivoni

2303

9.

xarcus

1820

10.

Anpanman

917

Become an Expert

Register today to share your knowledge with the community and be recognized and rewarded for your contributions.


Register Here




"Psst, Quomon is a great site. Pass it on."     Tell a Friend  |   Link To Us  |   Save to Delicious  |   Digg! Digg it



Language Options

English:

www.quomon.com

Español:

www.quomon.es