0

I want the user to be able to select the contents of a element by clicking it once. The code would look like this:

<div onclick="this.xyz()">...</div>

The question is: what method goes where I wrote xyz? I've searched for things like "DOM select object," but the answer is a needle hidden in a haystack of irrelevant hits (or not).

1
  • What do you mean "select the contents"? Do you want the contents passed to a function? Or do you want to copy them to the clipboard? Commented Feb 20, 2014 at 17:34

1 Answer 1

1

Basically you'd want:

<div onclick="var contents = this.innerText;">foo bar</div>

which would set contents equal to foo bar. Of course, this isn't exactly cross-platform compatible. Firefox expects .textContent instead of .innerText. If you're not opposed to using jquery, then

<div onclick="var contents = $(this).text()">foo bar</div>

would do just as well and be cross-platform.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.