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

263
Views
How can I use Javascript to simplify this code?

I started to learn Javascript and I'm building some sites to improve my skills. But here I would like to share with you a code that I couldn't simplify. As you will realize, I'm getting the ID of some DIVS to then add a event that go to differents categories. But, I can't find the way to make it easier without use all this code.



let entradas = document.getElementById("entradas");
entradas.addEventListener("click",irUrl1);
function irUrl1 () {
    window.location = "https://jikan.es/menu/entradas"
}


let veggie = document.getElementById("veggie");
veggie.addEventListener("click",irUrl2);
function irUrl2 () {
    window.location = "https://jikan.es/menu/veggie"
}


let rollsEspeciales = document.getElementById("rolls-especiales");
rollsEspeciales.addEventListener("click",irUrl3);
function irUrl3 () {
    window.location = "https://jikan.es/menu/especiales"
}


let pokeBowls = document.getElementById("poke-bowls");pokeBowls.addEventListener("click",irUrl4);
function irUrl4 () {
    window.location = "https://jikan.es/menu/poke-bowls"
}


let sashimi = document.getElementById("sashimi-niguiris");
sashimi.addEventListener("click",irUrl5);
function irUrl5 () {
    window.location = "https://jikan.es/menu/sashimi-niguiris"
}


let combinados = document.getElementById("combinados");
combinados.addEventListener("click",irUrl6);
function irUrl6 () {
    window.location = "https://jikan.es/menu/combinados"
}


let rollsClasic = document.getElementById("rolls-clasicos");
rollsClasic.addEventListener("click",irUrl7);
function irUrl7 () {
    window.location = "https://jikan.es/menu/rolls-clasicos"
}



let rollsSignature = document.getElementById("rolls-signature");
rollsSignature.addEventListener("click",irUrl8);
function irUrl8 () {
    window.location = "https://jikan.es/menu/rolls-signature"
}



let promociones = document.getElementById("promociones");
promociones.addEventListener("click",irUrl9);
function irUrl9 () {
    window.location = "https://jikan.es/menu/promociones"
}


    

</script>```
about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

You can do everything in one line:

document.querySelectorAll("#entradas,#veggie,#rolls-especiales,#poke-bowls,#sashimi-niguiris,#combinados,#rolls-clasico,#rolls-signature,#promociones").forEach(el=>el.addEventListener("click",()=>window.location = "https://jikan.es/menu/"+el.id.replace("rolls-especiales","especiales")));

There seems to be one exception to the rule which I have taken care of with the replace() function: the id #rolls-especiales needs to be redirected to especiales (without "rolls-").

However, the best solution would still be to simply use <a href="..."> tags of some kind in the html markup.

about 4 years ago · Santiago Gelvez Report

0

You can simplify the codes from:

let entradas = document.getElementById("entradas");
entradas.addEventListener("click",irUrl1);
function irUrl1 () {
    window.location = "https://jikan.es/menu/entradas"
}

to:

document.getElementById("entradas").addEventListener("click", function() {
    window.location = "https://jikan.es/menu/entradas";
});

or you can use jQuery:

$('#entradas').click(function() {
    window.location = "https://jikan.es/menu/entradas";
});
about 4 years ago · Santiago Gelvez Report

0

['entradas', 'veggie'].forEach(id => {
  document.getElementById(id).addEventListener('click', () => {
    window.location = `https://jikan.es/menu/${id}`
  })
})

this should reduce your repetition, but still I think the correct way to code would be to use anchor tags styled like your divs e.g.

<a style="whatever styles your div has" href="..." />
about 4 years ago · Santiago Gelvez 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!