4

I have some javascript, and either parentNode or previviousElementSibling seems to be breaking in IE8. The code works fine in firefox and IE9. This is the line that's not getting implemented:

$(submitter.parentNode.parentNode.previousElementSibling).children('#mark_as_broken').show();

code is something like

<form><div><input id=mark_as_broken></input></div></form>
<form><div><input id=mark_as_fixed></input></div></form>

where the mark_as_fixed input is the submitter. This works on other browsers.

any ideas which bit of it won't work, and why?

tiswas
  • 2,041
  • 7
  • 31
  • 43

1 Answers1

13

previousElementSibling is not supported until IE9.

http://www.quirksmode.org/dom/w3c_core.html#t84

Here's a function that should work. Haven't tested it yet. Seems to work.

var previousElementSibling = function( el ) {
    if( el.previousElementSibling ) {
        return el.previousElementSibling;
    } else {
        while( el = el.previousSibling ) {
            if( el.nodeType === 1 ) return el;
        }
    }
}

$( previousElementSibling(submitter.parentNode.parentNode) )

EDIT:

You didn't mention jQuery, but you appear to be using its API. If so, you can just do this:

$(submitter).closest('form').prev().find('#mark_as_broken').show();

Based on your markup, it appears as though you should be using .find() instead of .children().

user113716
  • 318,772
  • 63
  • 451
  • 440
  • can you explain this function - it seems to be recursive, though I am quite scrappy at javascript – tiswas Mar 04 '11 at 18:45
  • @tiswas: If the property is supported, it just uses that. If not, then as long as there's a `previousSibling`, it overwrites `el` with that, then tests to see if it is a type 1 element, and if so it returns it. If it gets to a point where there's no `previousSibling` at all, the `while` loop breaks, and `undefined` is returned by default. – user113716 Mar 04 '11 at 18:49