javascript - Why am I returned undefined? -
i wanting attribute of parent element cursor inside of.
i have been totally unsuccessful 2 hours now, i'm posting see if has suggestions. function have don't know why parentid
returns undefined:
function getattrofparent() { var newrange = rangy.getselection().getrangeat(0); var parentelement = newrange.commonancestorcontainer; var parentid = $(parentelement).attr('id'); alert(parentid); }
this works fine text of parent element...
function gettextofparent() { var newrange = rangy.getselection().getrangeat(0); var parentelement = newrange.commonancestorcontainer; var parenttext = $(parentelement).text(); alert(parenttext); }
...and works fine title of specified element.
function getattrofelement() { var parentid = $('#1').attr('id'); alert(parentid); }
here's jsfiddle, have click inside of text area first 2 functions work.
parentelement = newrange.commonancestorcontainer
isn't giving parent element, giving parent node.
this obvious if console.log(parentelement)
see being selected.
if select text entirely inside 1 of spans (which have ids) text node (which doesn't have id).
if select more text, paragraph element node (which doesn't have id).
you might want try var parentelement = $(newrange.commonancestorcontainer).parents('span');
Comments
Post a Comment