www.webdeveloper.com
Results 1 to 4 of 4

Thread: activeElement Safari

  1. #1
    Join Date
    Jun 2005
    Posts
    20

    activeElement Safari

    The following code will display the id of an input element in both IE and Safari. However, it will only display the id of a non-input div in IE. The alert message in Safari is blank. Could someone show me how to get the id of a div (non-input element) in Safari.

    Thanks,


    Code:
    window.addEventListener('click', function() {
    		divId(document.activeElement.id);
    }, false);
    
    function divId(div) {
          alert(div);
    }

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    I'm not familiar with activeElement or its support:
    Code:
    window.addEventListener('click', function( evt ) 
    {
      var srcElem = evt.srcElement || evt.target;
    
      while( srcElem && srcElem.nodeName !== 'DIV' )
        srcElem = srcElem.parentNode;
    
      if( srcElem )
        alert( srcElem.id );
    
    }, false);
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Jun 2005
    Posts
    20
    Thanks Ali, that works well. Would you mind explaining your code a bit.

    From what I can understand the function argument 'evt' is set by the element that is clicked. You then set the variable srcElem to either the element srcElement or target depending on whether the browser is IE or other.

    While srcElement is not a DIV (could be an input) it is set to the parent of the clicked element. Why 'while' and not 'if' here?

    If srcElem is a DIV then display id.


    Thank you.

  4. #4
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Quote Originally Posted by TedN View Post

    While srcElement is not a DIV (could be an input) it is set to the parent of the clicked element. Why 'while' and not 'if' here?
    The div may not be an immediate parent of the clicked element.
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles