Home > database >  Function is declared but its value is never read
Function is declared but its value is never read

Time:03-16

The function is working on the HTML page but not working from the external JS file and showing this error ('checkSelection' is declared but its value is never read). Is there any solution for this?

   function checkSelection(that) {
    if (that.value == "web-dev") {
      document.getElementById("web-section").style.display = "block";
    } else {
      document.getElementById("web-section").style.display = "none";
    }
    if (that.value == "grp-dsg") {
      document.getElementById("graphic-section").style.display = "block";
    } else {
      document.getElementById("graphic-section").style.display = "none";
    }
    if (that.value == "dgt-mkt") {
      document.getElementById("marketing-section").style.display = "block";
    } else {
      document.getElementById("marketing-section").style.display = "none";
    }
  }
<input onchange="checkSelection(this);">

CodePudding user response:

That is an error reported by the TypeScript checker in your IDE. To make it go away, this would be a workaround:

window.checkSelection = function(that) {
    if (that.value == "web-dev") {
      document.getElementById("web-section").style.display = "block";
    } else {
      document.getElementById("web-section").style.display = "none";
    }
    if (that.value == "grp-dsg") {
      document.getElementById("graphic-section").style.display = "block";
    } else {
      document.getElementById("graphic-section").style.display = "none";
    }
    if (that.value == "dgt-mkt") {
      document.getElementById("marketing-section").style.display = "block";
    } else {
      document.getElementById("marketing-section").style.display = "none";
    }
  }

The basic problem though is your use of inline event listeners, which is widely considered bad practice. Instead, I'd recommend you give that input an id and use addEventListener:

document.getElementById('section-picker').addEventListener('change', function() {
    if (this.value == "web-dev") {
      document.getElementById("web-section").style.display = "block";
    } else {
      document.getElementById("web-section").style.display = "none";
    }
    if (this.value == "grp-dsg") {
      document.getElementById("graphic-section").style.display = "block";
    } else {
      document.getElementById("graphic-section").style.display = "none";
    }
    if (this.value == "dgt-mkt") {
      document.getElementById("marketing-section").style.display = "block";
    } else {
      document.getElementById("marketing-section").style.display = "none";
    }
  });

If you take this advice, you need to make sure the Javascript executes after that input in the page is parsed. The easiest way to achieve that is by applying the defer attribute in the script tag.

CodePudding user response:

you need listen for the input first before using that in your function

let number = document.querySelector('input');

function check(that){
  if(that.value == 1){
    console.log('hello');
   }
   else{
    console.log('bye');
   }
 }
 
 number.addEventListener('input', ()=>{
  check(number);
 });
<input type="number">

take this for an example, you assign the input to a variable and then whenever that number is changed you run the function to check the value and display the result. same thing goes for checkbox too.

  • Related