2

Is there a way of selecting a section of HTML for copying using JavaScript, similar to how a user can do so with clicking and dragging sections of a website?

I've not been able to find anything about copying actual HTML areas, only input text, so I'm interested if this is actually possible or not.

I'm not looking for something that makes it look like it's selected, I'd like to actually select the HTML so that users can copy/ paste it into another application/ window.

Thanks!

AlbertEngelB
  • 16,016
  • 15
  • 66
  • 93
  • @JakeKing Not looking for text selection, I want HTML selected here. – AlbertEngelB Jun 03 '14 at 19:02
  • Look at the question/answer—it's about selecting HTML. – Alexis King Jun 03 '14 at 19:03
  • You looking for feature provided by gmail? When you select table then insert into mail and it looks like table? Not just splitted text? Or you just want to select complete markup with tags? – Uriil Jun 03 '14 at 19:03
  • @Uriil Similar, yes. I'd like to select the actual HTML/ text of the page so the user can Ctrl-C, Ctrl-X somewhere else. – AlbertEngelB Jun 03 '14 at 19:06
  • Here is what you possibly need: http://stackoverflow.com/questions/5973978/bookmarklet-which-captures-selected-content-including-html-tags – Uriil Jun 03 '14 at 19:09
  • @JakeKing Actually, I'm retarded, that function is exactly what I needed. :-X – AlbertEngelB Jun 03 '14 at 19:15

1 Answers1

0
var body = document.getElementsByTagName("body")[0];
var html = body.outerHTML

With the above snippet, you now have all of the HTML within (and including) the body tag. Do with it what you will.

ndugger
  • 7,373
  • 5
  • 31
  • 42
  • I'm not looking to do this programmatically. I'd like a user to be able to populate some HTML with data, then have a button to select the populated HTML to paste into another program. Thanks though! – AlbertEngelB Jun 03 '14 at 19:05
  • 1
    Then put it all into a textarea and have the user copy it from there. Forcing them to have it added to their clipboard isn't good design. Look into https://github.com/zeroclipboard/zeroclipboard though. – ndugger Jun 03 '14 at 19:06