0

I have an eventListener that won't allow me to pass parameters through it. I want to pass parameters through the addTextBox function. I tried passing it using the bind function and that does not working either. The code is below:

var jobSelect = document.getElementById("ctl01_TemplateBody_WebPartManager1_gwpciMyAccount_ciMyAccount_Affiliate_panelEditor_Demographics___JOB_TITLE");
    var jobBox = document.getElementById("ctl01_TemplateBody_WebPartManager1_gwpciMyAccount_ciMyAccount_Affiliate_panelEditor_Demographics___OTHER_JOB_TITLE");
    var jobLabel = jobBox.parentElement.previousSibling;

    jobSelect.addEventListener("change", addTextBox.bind(jobSelect,jobBox,jobLabel), false);

    function hideTextBox(){
        if(jobSelect)
        {
            if(jobSelect.value != "ZZ"){
                jobBox.style.visibility = "hidden";
                jobLabel.style.visibility = "hidden";
                jobBox.style.position = "absolute";
                jobLabel.style.position = "absolute";
            }
        }
    }

    function addTextBox(x,y,z){
        if(x){
            if(this.value == "ZZ"){
                y.style.visibility = "visible";
                z.style.visibility = "visible";
                y.style.position = "";
                z.style.position = "";
            }
            else{
                hideTextBox();
            }
        }
    }

                hideTextBox();
sjw0525
  • 329
  • 2
  • 17
  • The first argument to `.bind` is the `this` value, not the first argument to the function to be invoked. Pass `() => addTextBox(jobSelect,jobBox,jobLabel)` – CertainPerformance Aug 21 '20 at 05:13
  • So it would be?: jobSelect.addEventListener("change", addTextBox.bind(this,jobSelect,jobBox,jobLabel), false); – sjw0525 Aug 21 '20 at 05:15
  • 1
    That would work too, though you don't need to pass any value for the first argument. I'd prefer an anonymous function that invokes `addTextBox`, its intent is a bit clearer. – CertainPerformance Aug 21 '20 at 05:16
  • I tried with 'this' and that didn't work: jobSelect.addEventListener("change", addTextBox.bind(this,jobSelect,jobBox,jobLabel), false); – sjw0525 Aug 21 '20 at 05:19

0 Answers0