After re-reading your question countless times, suddenly it clicked: it seems like you want to use the code from div1
as a 'pattern' that you can re-use.
However your javascript has currently just one div-id hard-coded.
I didn't know what to make of div2
(from your function) so I changed that (as example) to a textarea
(with id
clipboardText
) that will show you the text-content that was just copied to the clipboard.
For simplicity (staying in-line of what seems to be your current code-logic/style) I suggest that the button is a direct child of the div that it needs to get the innerText
from. If this is the case (and we continue to use inline-script), you'd pass this
(that refers to the button that was clicked) to the function. In the function you'd then simply get the innerText
of the button's parentNode
:
Javascript:
function copyText(t) {
var copyDivText = t.parentNode.innerText;
var returnVal = window.clipboardData.setData('Text', copyDivText);
alert(returnVal);
document.getElementById('clipboardText').value = window.clipboardData.getData('Text');
}
HTML Body (note I added another 'pattern' with id div2
, since that seemed inline with what I guess you are doing):
<textarea id="clipboardText"></textarea>
<hr>
<div id="div1">
<div>
Positive Comments:
<br/>
<textarea id="txtArea" rows="3" name="txtCommentPositive#CurrentRow#">
#reReplaceCommentpositive
</textarea>
</div>
<div>
Negative Comments:
<br/>
<textarea rows="3" name="txtCommentNegative#CurrentRow#">
#reReplaceCommentnegative#
</textarea>
</div>
<input type="button" id="btn" value="copy" onclick="copyText(this);" />
</div>
<div id="div2">
<div>
Positive Comments:
<br/>
<textarea id="txtArea" rows="3" name="txtCommentPositive#CurrentRow#">
#reReplaceCommentpositive
</textarea>
</div>
<div>
Negative Comments:
<br/>
<textarea rows="3" name="txtCommentNegative#CurrentRow#">
#reReplaceCommentnegative#
</textarea>
</div>
<input type="button" id="btn" value="copy" onclick="copyText(this);" />
</div>
Hope this is what you intended with your question.
PS: if this is what you intended, note that a lot of people consider inline-scripts bad and when you dynamically attach your function you do not have to pass this
anymore (since then this
can be called directly inside your function).