javascript - previousSibling not working -


i want target first <p> inside div selecting second <p> , using previoussibling property not working.

<div id="par"> <p id="p1">test</p> <p id="p2">test</p> </div>   document.getelementbyid('p2').previoussibling.classname = 'red'; 

edit:

ok works on browsers except ie8 want work in ie8 well, tried following conditional no effect:

var c = document.getelementbyid('p2').previouselementsibling.classname = 'red';  if (c == undefined) {      c = document.getelementbyid('p2').previoussibling.classname = 'red'; } 

it still works everywhere ie8. how can change above conditional ie8?

edit 2:

managed work in ie8 well:

var c = document.getelementbyid('p2');  if (c.previouselementsibling) {      c.previouselementsibling.classname = 'red';  } else {      c.previoussibling.classname = 'red';  } 

you need use previouselementsibling previous element node.

document.getelementbyid('p2').previouselementsibling.classname = 'red'; 

http://jsfiddle.net/b6bh8/

note: not work on ie <= 8 according mdn link above. need loop through previoussibling until find element node.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -