/    Sign up×
Community /Pin to ProfileBookmark

Find the number of parentElements?

I am creating a program that uses an HTMLarea to input text. Once the text is inserted into the page, it is then draggable.

In order to make it draggable, I put the text string in a <DIV> tag with an ID. That ID is recognized and the text is draggable. This works fine, if the text was input alone. For example:

<div id=”some_id”>This is the text</div>

This text is referenced this way:

event.srcElement.id

But, if the text is altered in any way, it adds tags to it. For example:

<div id=”some_id”><strong>This is the text</strong></div>

Because of the <strong> tag, the ID needed for dragging now becomes the parentElement. It is now referenced this way:

event.srcElement.parentElement.id

This happens every time tags are inserted around the text. Obviously, no one can know how many tags a user is going to insert around the text. So, I need to know how I can dynamically find the number of parentElements, or how to create a work-around. Please let me know soon. Thank you.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@EwokNov 24.2004 — Try either CSS sheet or putting <div id="whatever" style="font-weight: bold;">

Ewok ?
Copy linkTweet thisAlerts:
@baconbuttyNov 24.2004 — jeep_man

Your code will be IE only.

However to do it, simply use parentElement as you have been doing.


E.g.

var eElement=window.event.srcElement;

while(eElement && eElement.id!="some_id")

{

eElement=eElement.parentElement;

}
Copy linkTweet thisAlerts:
@jeep_manauthorNov 24.2004 — Thanks, I'll try that.

By the way, I know it's only IE. I use:

e.target.id

e.target.parentNode.id

for the later versions of Mozilla browsers.

I just didn't want to type it all.
×

Success!

Help @jeep_man spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...