Adobe DTM and Event Listeners Multiplying Issue

89 Views Asked by At

I have a form and I'm trying to track 3 selects in the form and 3 text fields, using DTM. I can't get it to work properly? I tried an event-based rule first with my test code and the selects work as designed until I click on a text field. First time I click on a text field, the code works fine but then anytime after that when I click on any element with a listener, the code runs proportionally. For example, If I change the value in the text field, that code will run twice. If I then change the select, that code will run 3 times etc. I have also tried this code in a page load rule and the selects work fine but the text fields don't work at all. I have no idea how to fix this issue? Any advice is appreciated.

var employmentStatusSelect = document.getElementById('mainForm:cont');

 employmentStatusSelect.addEventListener('change',trackEmploymentStatusSelect);

  function trackEmploymentStatusSelect (){
        var esSelVal = event.target.value;
        console.log(" event.target.value ==> " + event.target.value);
        if(esSelVal != ""){
        console.log("employmentStatusSelect ==> " + event.target.value);  
    } else {
       console.log("employmentStatusSelect will not report");
    }
    employmentStatusSelect.removeEventListener('change', trackEmploymentStatusSelect);
};

var startCurrentJobInput = document.getElementById('mainForm:cont2');

startCurrentJobInput.addEventListener('blur', trackStartCurrentJobInput);

function trackStartCurrentJobInput(){
    var scjVal = event.target.value;
    console.log(" event.target.value ==> " + event.target.value);
    if(scjVal != ""){
        console.log("startCurrentJobInput ==> " + event.target.value);  
    } else {
       console.log("startCurrentJobInput will not report");
    }
    startCurrentJobInput.removeEventListener('blur', trackStartCurrentJobInput);
};
1

There are 1 best solutions below

0
On BEST ANSWER

I figured this out. Since it is an event-driven rule, the code reloads every time the rule criteria is met therefore I have to remove all listeners each time.