/    Sign up×
Community /Pin to ProfileBookmark

Marking a selection in IE

I want the user to select some text (in a static block which could include tables, images etc) and submit a form. I want to be able to identify what was selected, and thus highlight that text when it is displayed again (using PHP). The idea is that they can add comments and link them to the specific phrase in the text.

With Gecko, I decided the easiest way was (on submit) to insert some marker text at either end of the selection (e.g. [START], or something a little more sophisticated), copy the HTML to a hidden field, and then read this field after it is posted. Gecko provides all the text offsets to do this.

Not IE. The closest I could come up with was

[CODE] range=document.selection.createRange();
range.pasteHTML(‘[START]’+range.htmlText+'[END]’)[/CODE]

which falls over if one end happens to be in a different node to the other (e.g. selecting text across two table cells).

What’s the best way to achieve this in IE?

Many thanks,

Tim

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@timnbronauthorApr 26.2007 — Guess this may not be possible. I may have to resort to using previousSibling and parentElement to work out it's position in the heirarchy, submit that as a hidden input in the form (along with the actual text selected) and somehow match this up at the PHP end to work out which bit of text was highlighted. It could be a hit-and-miss process, because elements like TBODY appear in the DOM tree even if they're not in the HTML, and I suspect there will be others too!

Microsoft JavaScript bears a striking similarity to Visual Basic...
×

Success!

Help @timnbron 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 5.17,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...