SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Get parent element without jQuery

    I have all the scripts I need in place, but just need to complete this function, and without using a JS library:

    Code:
    function get_main_parent (elem) {
    
         //get the parent of the element (elem) that has the 'main-class' class and add a class to the first <div> in that parent element
    
    }
    The below will have an element triggered by click or focus, and that element will be passed into the above function (I only need to complete the function):

    (1) clicking the <a> link
    Code:
    <div class="main-class">
      <div>
    
       <span class='anything'>
         <a id='this-element' href="">Click</a>
       </span>
    
      </div> 
    </div>
    (2) focusing in an input box
    Code:
    <div class="main-class">
      <div>
    
        <input type="text" />
    
      </div> 
    </div>
    --

    All of them then add the class, so example 2 would then become:
    Code:
    <div class="main-class">
      <div class="the-new-class-added">
    
        <input type="text" />
    
      </div> 
    </div>
    ..basically the event could be triggered anywhere, and any number of elements deep. The function just needs to receive an element and go up the DOM tree to find the div with 'main-class' as the class and then step forward one to find the first <div> and add a class to that.

    I'm sure there's a better way to explain this. Thanks.

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by johnsmith153 View Post
    ..basically the event could be triggered anywhere, and any number of elements deep. The function just needs to receive an element and go up the DOM tree to find the div with 'main-class' as the class and then step forward one to find the first <div> and add a class to that.

    I'm sure there's a better way to explain this. Thanks.
    Perhaps with this pseudo code:

    Code:
    (function () {
        "use strict";
        
        // hasClass, addClass and removeClass from http://snipplr.com/view/67176/class-handling-functions/
    
        function hasClass(ele, cls) {
            return ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'));
        }
    
        function addClass(ele, cls) {
            if (!hasClass(ele, cls)) {
                ele.className += ' ' + cls;
            }
        }
    
        function removeClass(ele, cls) {
            if (hasClass(ele, cls)) {
                var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)'),
                    newClass = ele.className.replace(reg, ' ');
                ele.className = newClass.replace(/^\s+|\s+$/g, '');
            }
        }
    
        function getMainClassElementAbove(element) {
            var parentElement = element.parentNode;
            while (!hasClass(parentElement, 'main-class') && parentElement.nodeName !== 'BODY') {
                parentElement = parentElement.parentNode;
            }
    
            return parentElement;
        }
    
        function getDivBelow(element) {
            return element.querySelector('div');
        }
    
        function getDivBelowMainClass(element) {
            var mainClassElement = getMainClassElementAbove(element);
    
            return getDivBelow(mainClassElement);
        }
    
        function addClassToFirstDivOfMainClass(evt) {
            evt = evt || window.event;
            var clickedElement = evt.target || evt.srcElement,
                target;
    
            target = getDivBelowMainClass(clickedElement);
            addClass(target, 'the-new-class-added');
        }
    
        var containers = document.querySelectorAll('.main-class'),
            i;
        for (i = 0; i < containers.length; i += 1) {
            containers[i].onclick = addClassToFirstDivOfMainClass;
        }
    }());​
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

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