troubleshooting Question

How to Attach/Remove Click Event Listener to Button Element Dynamically

Avatar of 894359
894359Flag for United States of America asked on
JavaScriptWeb Development* HTML 5
4 Comments1 Solution91 ViewsLast Modified:
This is related to a recent question I posted, regarding the below code:
<div class="toggle-fieldView">
        <button type="button" data-value="Fund Now" class="">FUND NOW</button>
        <button type="button" data-value="Fund Later" class="active">FUND LATER</button>
This button element is loaded dynamically in the user flow after a specific view is displayed. How do I dynamically attach the "click" event to these two buttons?  I want to monitor when they are clicked but I want to remove the listener once the view changes? This is what I'm currently testing with but I need to be able to attach and then remove the listener? How do I do this?
    function _dtmCheckOAO4FundingMethod(){
        document.body.addEventListener('click', function(event) { //Need to fix and attach when needed
	        if(( == "button")){ 
                if( == "Fund Now"){
                    sessionStorage.setItem("_dtmFundType", "fund now");
                    sessionStorage.setItem("_dtmFundTypeDetail", ":Funding Method:fund within 30 days");
                if( == "Fund Later"){
                    sessionStorage.setItem("_dtmFundType", "fund later");
                     sessionStorage.setItem("_dtmFundTypeDetail", ":Funding Method:Credit Card");
I'm assuming that I 'd have to have two different assignments (one for each button) and manage separately?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 4 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros