Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

parentElement problem

Status
Not open for further replies.

IndyFusion

Programmer
Jan 9, 2003
7
0
0
US
If I have code like this:
Code:
<OL>
  <LI>
    <FONT face=Arial color=black size=2>asdf</FONT>
  </LI>
</OL>
and I'm using the javascript:

var _currEl = objDOM.selection.createRange();
var myobj = _currEl.parentElement();
var myobj2 = myobj.parentElement();

alert(myobj2.tagName);

Why do I not get any script errors and absolutly nothing happens. If I alert the tagName of myobj it returns 'FONT'. I'm trying to get the parent of the FONT element to see if it is a list item. What am I doing wrong? I've searched everywhere and have been working on this very simple problem for way too long. I consider myself to be a pretty good javascript programmer, but I must be overseeing something or not understanding how the parentElement method works. I would appreciate ANY help. Thank you.
 
Try
Code:
var myobj2 = myobj.parentNode;
.

The
Code:
parentElement()
method in the code would be extremely difficult to implement -- given an element, construct its parent and insert it into the DOM tree. That's probably why the DOM implementors elected to go with
Code:
appendChild()
instead.
 
Actually, I found an easy was to do this on Googles Groups. Loop through the parents until I find an OL or BODY element. I have tested the code below and it works very well.

var objDOM = element.editor.DOM;
var _currEl = objDOM.selection.createRange();
var objParentElement = _currEl.parentElement();

if (objParentElement)
{
while(objParentElement.tagName != &quot;BODY&quot;)
{
if (objParentElement.tagName == &quot;OL&quot;)
{
break;
}
else
{
objParentElement = objParentElement.parentElement;
}
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top