Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
80
rated 0 times [  85] [ 5]  / answers: 1 / hits: 25265  / 14 Years ago, wed, january 12, 2011, 12:00:00

For example for this.parentNode I would like to just write this.p or instead of
document.getElementById('someid') just write document.g('someid'). Of course that are simple examples, I just want to know what is the correct way to do it.



(I know I can use jQuery or Prototype, but I'd like to learn how it is really done in JS)


More From » methods

 Answers
15

Although you can prototype on the HTMLElement in many browsers - Internet Explorer (6,7,8) is NOT one of them. AFAIK, IE9 does support this (though I haven't tested it).


For browsers that do handle it, you can do:




HTMLElement.prototype.doHello = function(thing){
console.log(this + ' says: ' + thing)
}

document.body.doHello('hello')




[#94256] Tuesday, January 11, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dominics

Total Points: 424
Total Questions: 99
Total Answers: 107

Location: South Korea
Member since Fri, Sep 11, 2020
4 Years ago
dominics questions
Wed, Apr 6, 22, 00:00, 2 Years ago
Thu, Jan 13, 22, 00:00, 2 Years ago
Fri, Sep 18, 20, 00:00, 4 Years ago
;