Solution 1 :

Using querySelectorAll, querySelector and getElementById should get you started.

// get "data-qa" value by "id"
var data = document.getElementById('id-test2').getAttribute('data-qa');
console.log(data);

// get "id" value by "data-qa"
var data = document.querySelector('[data-qa="data-qa-test2"]').id;
console.log(data);

// get all elements with attribute "data-qa"
var elems = document.querySelectorAll('[data-qa]');

// get all "ids"
var data = [];
elems.forEach(function(elem){
  data.push(elem.id);
});
console.log(data);

// get all "data-qa"
var data = [];
elems.forEach(function(elem){
  data.push(elem.getAttribute('data-qa'));
});
console.log(data);

// get all "ids" and "data-qa"
var data = [];
elems.forEach(function(elem){
  data.push({
    "id": elem.id,
    "qa": elem.getAttribute('data-qa')
  });
});
console.log(data);

// get all "ids" and "data-qa" with "id" as key
var data = {};
elems.forEach(function(elem){
  data[elem.id] = {
    "id": elem.id,
    "qa": elem.getAttribute('data-qa')
  };
});
console.log(data);
<div id="id-test" data-qa="data-qa-test"></div>
<div id="id-test1" data-qa="data-qa-test1"></div>
<div id="id-test2" data-qa="data-qa-test2"></div>
<div id="id-test3" data-qa="data-qa-test3"></div>

Solution 2 :

Is this what you mean, the question is a little confusing.

const ids = ["id-test","id-test1","id-test2","id-test3"];

const getData = (id) => document.querySelector(`#${id}`).dataset.qa;

const values = ids.map(id => getData(id));

console.log(values);
<div id="id-test" data-qa="data-qa-test"></div>
<div id="id-test1" data-qa="data-qa-test1"></div>
<div id="id-test2" data-qa="data-qa-test2"></div>
<div id="id-test3" data-qa="data-qa-test3"></div>

Problem :

Using only JavaScript, without the use of JQuery etc, How do I find the corresponding data attribute given an Id ?

Example

<div id="id-test" data-qa="data-qa-test">
</div>

Input: "id-test"

Output: "data-qa-test"

Is there a way in Javascript to take the code below, and find the corresponding data-qa automation tag locators?

Related question:

Javascript: Select all data-qa attributes on HTML Page

Background: Trying to replace Selenium ID locators, with our data qa attributes.

Comments

Comment posted by Taplar

Given an id, use the id. Ids cannot repeat. If your markup is repeating ids, it is invalid by web standards and should be fixed.

Comment posted by Bibberty

Then the code works, look at the console output. The function

By