This project is archived and is in readonly mode.

#482 ✓invalid
Russell Chappell

Checking if element parent is defined results in error

Reported by Russell Chappell | November 19th, 2008 @ 02:38 PM | in 2.0 (closed)

I've been doing some checks for elements to see if it has a parent tag defined. My code was for a Click event

if($defined(element.getParent('li'))) { // Do Something }

This results in an error basically saying that getParent('tag') is unknown object / property, surely if getParent() doesn't find anything it should return false to stop any errors?

Or perhaps to include some new Element functions such as hasParent(), hasParents()

Comments and changes to this ticket

  • Jan Kassens

    Jan Kassens November 19th, 2008 @ 03:08 PM

    • State changed from “new” to “invalid”
    • Assigned user changed from “Valerio” to “Jan Kassens”

    did you use event.target.getParent(tag)? If that's the case you need to extend the event target with $: $(event.target).getParent(tag)

    If not, please reply here, will reopen than. (A testcase attached would sure help than).

  • Russell Chappell

    Russell Chappell November 19th, 2008 @ 04:38 PM

    I used:

    var element = $(event.target);

    then performed my code above and it still results in an error. Here is the full code below:

    $(document.body).addEvent('click', function(event) {

    var el = $(event.target);
    // Check clicking on list item
    if($defined(el.getParent('li'))) {
        event.stop();
        // Do something
    };
    
    // Check clicking on anchor within id: menu
    if($defined(el.getParent('a').getParent('#menu'))) {
        event.stop();
        // Do Something
    }
    
    

    )};

  • Russell Chappell

    Russell Chappell November 19th, 2008 @ 04:41 PM

    Also:

    Hope this helps. getParent(), getParents() without specifying a tag doesnt result in an error. I've been using the functions below to test without generating any errors:

    function hasParentTagAndClass(theTarget, theTag, theClass) {

    returnme = false;
    
    theTarget.getParents().each(function(el) {
    
        if(el.get('tag') == theTag) {
            if(el.hasClass(theClass)) {
                returnme = true;
            }
        }
    
    });
    return( returnme );
    
    

    } function hasParentTagAndId(theTarget, theTag, theId) {

    returnme = false;
    
    theTarget.getParents().each(function(el) {
    
        if(el.get('tag') == theTag) {
            if(el.getProperty('id') == theId) {
                returnme = true;
            }
        }
    
    });
    return( returnme );
    
    

    } });

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Shared Ticket Bins

Pages