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

112
Views
Function is defined but never used in the script section of a Vue component

I am trying to manage my current website using Vue and more particularly Vue Router. As I am already using Bootstrap, I need some Javascript to manage the state of my buttons. The following code only works if I comment the script section.

<template>
    <div class="container">
          <header class="d-flex justify-content-center py-3 border-bottom">
              <ul class="nav nav-pills" id="navbar_nav">
                  <li class="nav-item"><router-link to="Add" type="button" class="nav-link" onclick="update_nav_bar(this);">Add</router-link></li>
                  <li class="nav-item"><router-link to="Edit" type="button" class="nav-link" onclick="update_nav_bar(this);">Edit</router-link></li>
              </ul>
          </header>
  </div>
  <router-view/>
</template>

<script>
let navbar_nav = document.getElementById("navbar_nav");
function update_nav_bar(elem) {
    navbar_nav.getElementsByClassName("active")[0].classList.remove("active");
    elem.classList.add("active");
}
</script>

If I do not comment it, I get this error:

15:10  error  'update_nav_bar' is defined but never used

So far I removed a lot of getElementById calls in my code thanks to Vue. So I guess there is a more elegant (and working...) way to achieve what I want, but I am new to Vue and lack any hindsights. Could you help ?

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

0

You have to define the function in the methods object. Or - if you use the composition-api - return the function from the setup fuction.

<script>
let navbar_nav = document.getElementById("navbar_nav");

export default {
  methods: {
    update_nav_bar(elem) {
      navbar_nav.getElementsByClassName("active")[0].classList.remove("active");
      elem.classList.add("active");
    }
  }
}
</script>

But maybe you should rethink the function in general. Usually you can achieve this without manipulating the DOM manually with JavaScript.

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!