lionHeart
lionHeart

Reputation: 647

Javascript returned object properties undefined

These are some relevant HTML elements that I work with:

                <TD id=Cell.0.0>
                   <DIV></DIV>
                   <IMG id=_Q0_C0_mrSingleImg src="radio.gif">
                   <INPUT type="checkbox" name=_QGender_C class=mrSingle id=_Q0_C0> 
                   <LABEL for=_Q0_C0>
                      <SPAN class=mrSingleText >
                        some text
                      </SPAN>
                   </LABEL>
                </TD>

In my main JS file, I first assign this onClick method to the img element:

function onImageClickSingle(event) {

    // catch clicked img element
    event = event || window.event;
    var img = event.target || event.srcElement;  // IE fix
    if (img.nodeType == 3) img = img.parentNode; // firefix bug fix

    var inputSibling = getNextSibling(img);

    // some more code that involves working with inputSibling attributes...
}

As you can see, in the above function I call "getNextSibling" to get the image sibling, that is the input element.
Since the code must support older version of IE, I added this "getNextSibling" function to my fix.js file, as was recommended on another stack overflow thread:

// IE 8 and less do not support nextSibling or previousSibling javascript properties 
function getNextSibling(element) {
    var sibling = $('#' + element.id + '').next();
    return sibling;
}

The problem I encounter is that when debugging code, I can see that inputSibling variable does contain the requested input sibling element that "getNextSibling" returns (id, checked, class name etc, are all assigned a value), but when I try to work with either of the returned object's attributs, they are all undefined.

Anyone has an idea of why this might happen?

Upvotes: 3

Views: 117

Answers (1)

volkinc
volkinc

Reputation: 2128

your function returns jQuery object

function getNextSibling(element) {
    var sibling = $('#' + element.id + '').next();
    return sibling;
}

so you will be able to access the attributes using attr()

inputSibling.attr('id')

or if you want to continue with JS access style you need to

inputSibling.get(0).id

Upvotes: 1

Related Questions