Home > other >  Javascript Function - Return Response
Javascript Function - Return Response

Time:10-28

I am new to javascript and trying to run a javascript function and return a response for this website below. I am trying to use 'document.querySelectorAll' to return a response for all the job titles and the URL for each job posting. How do I return a response for these elements?

Website: https://mckesson.wd3.myworkdayjobs.com/External_Careers

Here is my latest attempt:

function ExecuteScript() {
 let nameList = [];
 let response = '';

 document.querySelectorAll('a[data-automation-id="jobTitle"]').forEach((element, i) => {
  nameList.push(element.innerHTML);

 document.querySelectorAll('a[data-automation-id="jobTitle"]').forEach((element, i) => {
  response  = nameList[i]   '\\t'   element.getAttribute('hef')   '\\n';
 });

 return response;
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

There are several problems with the example code:

  1. Syntax error: Two querySelectorAll are not nested correctly.

  2. Misspellings: hef should be href, and it's not enough to store only relative addresses; you need to add domain names in front of them.

  3. Because both capture elements of the same class, the two loops can be merged.

Can refer to the following code:

function ExecuteScript() {
  let response = '';
  document.querySelectorAll('a[data-automation-id="jobTitle"]').forEach((element, i) => {
    response  = element.innerHTML   '\\t'   location.host   element.getAttribute('href')   '\\n';
  });
  return response;
}
  • Related