-2
<div class="QnS3qe OUw7Vd yIinVd rAR0Uc" data-oid="100018429050627276346" jsaction="click:hFzPaf;"><div class="qt0M0c"><img class="jJzip" 
<div class="QnS3qe OUw7Vd yIinVd rAR0Uc" data-oid="103817096990403408962" jsaction="click:hFzPaf;
<div class="QnS3qe OUw7Vd yIinVd rAR0Uc" data-oid="1038170969904034563" jsaction="click:hFzPaf;
<div class="QnS3qe OUw7Vd yIinVd rAR0Uc" data-oid="103817096990403408807" jsaction="click:hFzPaf;

How I can get number in " data-oid= ..." with javascript ?

TRAM COK
  • 11
  • 3
  • You need something that distinguish that div from other divs on the page. How are you going to do that? You could do a querySelector on `[data-oid]`, but I presume that there exist other divs with that tag. – Rickard Elimää Jul 18 '21 at 09:23
  • [Select the element](https://stackoverflow.com/questions/3808808/how-to-get-element-by-class-in-javascript), then [get its data attribute](https://stackoverflow.com/questions/33760520/how-can-i-get-the-values-of-data-attributes-in-javascript-code). – Ivar Jul 18 '21 at 09:26

1 Answers1

0

For single element:

const el = document.querySelector("[data-oid]");

if(el != undefined && el != null){
    let value = el.getAttribute("data-oid");
    console.log(value); // prints out 100018429050627276346
}

For both multiple and single element:

const els = document.querySelectorAll("[data-oid]");
let value;

if(els != undefined && els != null && els[0] != undefined){
   for(let el of els){  
       value = el.getAttribute("data-oid");
       console.log(value); // prints out the values
   }
}
Ar Rakin
  • 534
  • 3
  • 15