Reply
Copy Content of Div or Span with Javascript
Old 07-15-2008, 05:52 PM Copy Content of Div or Span with Javascript
aschecht's Avatar
Extreme Talker

Posts: 180
Name: A
Location: San Jose, CA
I'm using a Javascript that allows users to click a button on my site which copies any selected content to the clipboard.

I'm wondering if it's possible to save a step so that the Javascript allows them to click on any text content on the webpage and, with the click, have the content in the div or span they're clicking on copied to the clipboard?

In other words, I want to save them the trouble of highlighting the content and then clicking the button. I want them, just by clicking on a word in that div or span to have it select all the content of that div/span and copy it to the clipboard.

Can it be done?

Andrew
aschecht is offline
Reply With Quote
View Public Profile Visit aschecht's homepage!
 
When You Register, These Ads Go Away!
Old 07-15-2008, 06:30 PM Re: Copy Content of Div or Span with Javascript
chrishirst's Avatar
Super Moderator

Latest Blog Post:
Am I impressed or what?
Posts: 14,899
Location: Blackpool. UK
http://www.htmlgoodies.com/beyond/ja...le.php/3458851
http://www.dynamicdrive.com/dynamicindex11/copytext.htm

Although it will only work in IE and possibly Opera
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 07-15-2008, 07:35 PM Re: Copy Content of Div or Span with Javascript
aschecht's Avatar
Extreme Talker

Posts: 180
Name: A
Location: San Jose, CA
Thanks for the quick reply, Chris. It doesn't look like either of those scripts will exactly fit the bill for me. Here's an example of one of the pages I'd like to implement this on:
http://www.icdmeister.com/site/Codes...ential_HTN.htm
I want users to be able to click on any of the red numbers and have that click select and copy the full text in that particular <span>

The first script (http://www.htmlgoodies.com/beyond/ja...le.php/3458851)
allows one to click a button to copy all the text in a pre-defined area. It doesn't use the click to determine which area/content is copied from.

The second script
(http://www.dynamicdrive.com/dynamicindex11/copytext.htm)
gets rid of the button but still requires the selection of text by dragging the mouse. I'd like to be able to select that text just by clicking anywhere within that span or div's text.

My current copy to clipboard script works with IE and FF so I'm hoping I'll be able to adapt it to maintain compatibility with both browsers.

I guess a more general question is: Is it possible to select the content in a div/span by clicking on it or does selection require either dragging over the area with the mouse or having it in a pre-defined content area?

Andrew
aschecht is offline
Reply With Quote
View Public Profile Visit aschecht's homepage!
 
Old 07-16-2008, 12:19 AM Re: Copy Content of Div or Span with Javascript
chrishirst's Avatar
Super Moderator

Latest Blog Post:
Am I impressed or what?
Posts: 14,899
Location: Blackpool. UK
to get the content of a particular element use this.innerHTML in an onclick event and pass it to your script
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 07-16-2008, 02:25 AM Re: Copy Content of Div or Span with Javascript
aschecht's Avatar
Extreme Talker

Posts: 180
Name: A
Location: San Jose, CA
Thanks Chris. It looks like what I want to do will be possible with just some small modifications to my original Copy to Clipboard script.

Thanks for pointing me towards this.innerHTML. Looks like it should do the trick.

Andrew
aschecht is offline
Reply With Quote
View Public Profile Visit aschecht's homepage!
 
Reply     « Reply to Copy Content of Div or Span with Javascript
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.12672 seconds with 12 queries