Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
Add class to the last four "a" elements in javascript
<a id="aitems" target="_blank" rel="noopener noreferrer" href="<?= $block->getItemUrl($item)?>"><?=$item['name']?>

I have this "a" tag that is rendering 5 elements that are menu options. But I need that 4 of those 5 elements to open a new url on a different tab that's why I've added the target class. But of course if I do this way then the first "a" element will open on a new tab too and I dont want that.

This is what I'm trying to do

 function addClass(){
        const list = document.querySelector('#aitems');

        // list[0].removeClass("target");
        list[0].addClass("test1");
        list.firstElementChild.classList.add("test");
    }
    addClass();

I'm just trying or to remove the class target from the first element or add the class target to the other 4 elements. It's always giving me errors that can't read classList or addClass. I've already tried with getElementById or byClassName

I'm doing this on a .phtml file.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

So I eventually solved the problem ill leave it here if someone needs it

<a class="model-a" target="_blank" rel="noopener noreferrer" href="<?= $block->getItemUrl($item)?>"><?=$item['name']?>
</a>
function addClass() {
  var divs = document.querySelectorAll('.model-a');
  for (var i = 0; i < divs.length; i++) {
    divs[0].classList.add('newclass');
    divs[0].removeAttribute('target');
  }
}
addClass();

The classList.add is not necessary but I will leave it.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!