Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    74
    Thanks
    3
    Thanked 3 Times in 3 Posts

    adding custom method to DOM element in ie8

    I've been trying to add a custom method, firstElementChild, to DOM Elements in ie8. I can't figure out what object to add it to. Here's what I have:

    Code:
    function check_firstElementChild()
    {
    	if(!Element.prototype.firstElementChild)
    	{
    		Element.prototype.firstElementChild = function()
    		{
    			var retval = false
    			for(var x=0;x<this.childNodes.length;x++)
    			{
    			
    				if(this.childNodes[x].nodeType == 1)
    				{
    					retval = this.childNodes[x]
    					break
    				}
    			}
    			return retval
    		}
    	}
    }
    I also tried Object in place of Element above. ie8 keep saying:

    undefined

    when I put:

    document.getElementById('mydiv').firstElementChild

    in the console. Is it possible to do this in ie8? I some research and several sites said this was supported in ie8. Thanks.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Answer removed as being horribly obsolete. *sigh*
    Last edited by Old Pedant; 02-25-2013 at 10:11 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by Old Pedant View Post
    You can't add methods to DOM elements. Period.
    ]
    WTF? dude, you should know better than that...
    That's one of the main things IE8 added!

    besides, OP wants a property, not a method, not that it matters since we can do both.

    All DOM elements (no matter how they are defined) inherit from both Node and Element, and even Object eventually...

    OP: here's the code to add the property on to all elements if missing:

    Code:
    if(document.body.firstElementChild===undefined){
    
    Object.defineProperty(
      Element.prototype, 
      "firstElementChild", 
      { get:  function getFirstChild(){	
           return this.getElementsByTagName("*")[0];
       }
     });//end odp
    
    }//end if
    
    //demo:
    alert(
       document.body.firstElementChild.nodeName
    );
    works in IE8+everything new
    Last edited by rnd me; 02-25-2013 at 10:01 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Quote Originally Posted by rnd me View Post
    WTF? dude, you should know better than that...
    That's one of the main things IE8 added!
    REALLY? WOW!

    Okay, my IE knowledge is clearly WAY out of date.

    I admit that the only time I had access to IE source code was way back in version 4 days (saw some of the early version 5 code).

    Okay...strike my answers.

    I'm deleting them!
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New Coder
    Join Date
    Mar 2009
    Posts
    74
    Thanks
    3
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by rnd me View Post
    WTF? dude, you should know better than that...
    That's one of the main things IE8 added!

    besides, OP wants a property, not a method, not that it matters since we can do both.

    All DOM elements (no matter how they are defined) inherit from both Node and Element, and even Object eventually...

    OP: here's the code to add the property on to all elements if missing:

    Code:
    if(document.body.firstElementChild===undefined){
    
    Object.defineProperty(
      Element.prototype, 
      "firstElementChild", 
      { get:  function getFirstChild(){	
           return this.getElementsByTagName("*")[0];
       }
     });//end odp
    
    }//end if
    
    //demo:
    alert(
       document.body.firstElementChild.nodeName
    );
    works in IE8+everything new
    Thank you!!


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •