-1
<a href="javascript:" class="btn btn-default btn-xs btn-email-copy" data-id="8057492" data-type="receipt_email" data-hash="76af85cb574e88b8c802f56044a06b9f" title="" data-original-title="test">test</a>

I click on the button to copy email in clipboard. Now I'm trying to catch in a variable the value of the clipboard.

var test = document.getElementByName('8057492');

but console says the value is null ... I tried to get access to the object a.btn.btn-default.btn-xs.btn-email-copy but I don't know what to do then?

Cjmarkham
  • 9,484
  • 5
  • 48
  • 81
magique
  • 1
  • 1
  • There's no `document.getElementByName()` method. Based on the title I would guess -> [Why does jQuery or a DOM method such as getElementById not find the element?](https://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – Andreas Mar 24 '20 at 17:29

2 Answers2

0

There is no name attribute, or id attribute in the HTML you have provided. Because these attributes are not present in your HTML, and selector that references them will return null.

It looks like you are trying to obtain the element by reference to the data-id="8057492" attribute of the sample <a> tag. This attribute is a data attribute, not an ID.

You can access data attributes by using the JavaScript query selector function to select the first element on a page whose data-id attribute is equal to 8057492:

document.querySelector("*[data-id='8057492']");
TS89
  • 116
  • 7
  • When I try your solution : --> undefined test – magique Mar 26 '20 at 00:11
  • Have you literally just typed 'document.querySelector("*[data-id='8057492']");' into your web browser console and copied the output? If so, it looks you have correctly selected the element using the querySelector function (hence the HTML you wanted to select has been repeated back to you). – TS89 Mar 26 '20 at 00:34
  • It isn't clear from your question what you actually want to achieve by getting the element. I assume you are trying to create a copy paste function. If so, I would suggest looking at the [MDN resources on the JavaScript clipboard API](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Interact_with_the_clipboard). If you have any issues building such a function, it would be best to create a new question specifically about that issue. – TS89 Mar 26 '20 at 00:36
  • here is an example to show you how it is working : Go to: https://www.peoplenjob.com/jobs/4057492 Click on the javascript button next to the picture "hailey@nowbest.com" (이메일 복사) Check the clipboard content. My objective is to get the content of the clipboard. – magique Mar 26 '20 at 01:46
  • Understood. In that case, the question you have asked (accessing DOM) is not actually what you are try to achieve (i.e. to paste a value from the clipboard), and you would get better answers be rewriting or asking a fresh question. The best starting point is the [MDN article I mentioned before](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Interact_with_the_clipboard). However, the reality is you will not have much success as the implementation of the paste function is very limited due to concerns around security. – TS89 Mar 26 '20 at 09:13
0

oYou try to get the

 data-id="8057492"

So you either create an id-tag

id="8057492"

<a href="javascript:" id="8057492" class="btn btn-default btn-xs btn-email-copy" data-id="8057492" data-type="receipt_email" data-hash="76af85cb574e88b8c802f56044a06b9f" title="" data-original-title="test">test</a>

and do

var test = document.getElementById("8057492");
var testValue = test.innerHTML;  // Here we got the email address if its >test<

could also be done in one step if test has to contain the email address

 var test = document.getElementById("8057492").innerHTML;

now your console should display correct data. If you have to use data-id the the solution is more complex, but solveable. I would go for an unique ID like id = "emailuser" to select that
If you have varying data-ids or you go for the class selector btn....email If there is only one on the page it woould be

var testVar= document.getElementsByClassName("btn...email");
var test = testVar[0];
Codebreaker007
  • 2,911
  • 1
  • 10
  • 22