0

Here's my code

when user click on this Button

<a href="javascript:void(0)" onClick="someFunction()">Copy</a>

The Text Inside This Div To Copy. How Can I Do This??? Sorry For My Bad English

<div>Some Text Here...</div>
Alex
  • 1,451
  • 5
  • 15
  • 16

2 Answers2

0

Assuming that you are attempting to copy to another HTML element or variable

First, it may help you to give your div an id so that you can easily identify it using javascript:

<div id="div1">Some Text Here...</div>

If you are trying to use a button instead of a hyperlink, you would then create the button:

<button id="button1">Click to copy</button>

Then, if you are using jQuery, you could do the following in your function:

var myVar;
$( "#button1" ).click(function() {
     // If you want to store the data in another HTML element
     $( "#div2" ).html( $( "#div1" ).html() );

     // If you want to store the data in a js var
     myVar = $( "#div1" ).html();
});

Here is a Plunkr

Sidenote, hyperlinks are not the same as buttons. They are clickable, but a button has a completely separate tag in markup.

Community
  • 1
  • 1
Evan Bechtol
  • 2,855
  • 2
  • 18
  • 36
0

To copy text from a Div, you'll want either a class or ID to locate that div. If you're doing this with JavaScript, it would look something like this:

[HTML code]
<div id="test">This is the div text</div>
<a href="#" onclick="retrieveDivText()">Click me</a>

And in your JavaScript:

  function retrieveDivText() {
  var divElement = document.getElementById('test');
  var divText = divElement.innerHTML;
  alert(divText);
}

Of course, you can return that divText variable (instead of just alerting it), or do whatever you want with it.

Example here: http://codepen.io/anon/pen/jqzvMp?editors=1010

Gunderson
  • 3,263
  • 2
  • 16
  • 34