locked
Event not switching off RRS feed

  • Question

  • User1568384800 posted

    I have a button who's event is control on a main menu partial page.

    //Next Button
    AssignNextBtn();
    
    function AssignNextBtn() {
                
                $('#LoadSpecialAuditDiv').off('click', '#NextBtn');
               
                $('#LoadSpecialAuditDiv').on('click', '#NextBtn', function () {
                    GetNextScreen();
                })
            }

    On a particular partial page, when the page is loaded in and loads it turns off the event and then creates a new one.

     $('#LoadSpecialAuditDiv').off('click', '#NextBtn');
    
     $('#LoadSpecialAuditDiv').on('click', '#NextBtn', function () {
                
                if ($('#StockPeriodStartTxt').attr('disabled') || ValidateLRPage1Controls()) {
                        //Load init for page 2.
                        PreLoadPage2();
                }
      })

    The click event on the loaded page has stopped turning the event off.

    This used to work fine, now it seems to have stopped, not sure what has changed. Any ideas?

    Wednesday, April 18, 2018 10:55 AM

Answers

  • User475983607 posted

    Mgebhard, theres a lot of code associated with this which is why I've culled the amount I've posted. Through debugging I've found the event is no longer being switched off. 

    The Main menu page has a series of buttons - the main page has previous and next buttons at the bottom. The code looks for what the next button is in the main menu and loads that when the next button is pressed. On one page in particular, I decide I don't want it the next button to go to the next page in the series of buttons I want it to show a second page page (Theres a lot of questions on the page) so page 2 of 2. So when page one loads it unassigns the event from the new one, loads and hides what will be page 2 and assign a new event to the net button.

    I hope that makes sense.

    It makes sense but we cannot debug code that we cannot see.  You'll need to use the debugging tools to find and fix the bug or post code that reproduces the issue.

    Why are you using JavaScript?  Is this an SPA?  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 18, 2018 11:58 AM
  • User1568384800 posted

    You're right, I should have posted all or at least more of the code, as although I'd debugged it to that point, it was a bit of a red herring as it was the on ready event causing the problem for it. 

    Thanks for your time.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 18, 2018 3:56 PM

All replies

  • User475983607 posted

    Your question is not clear and the code is very confusing and incomplete.  Can you explain at a high level what problem you are trying to solve?

    Have you tried basic debugging?  Press F12 to open the browser's developer tools and look for errors in the console.  Set a breakpoint and step through the code.

    Wednesday, April 18, 2018 11:13 AM
  • User516094431 posted

    As far as I known, You should use following line of code.

    $(document).on('click','#NextBtn',function(){
        if ($('#StockPeriodStartTxt').attr('disabled') || ValidateLRPage1Controls()) {
                        //Load init for page 2.
                        PreLoadPage2();
                }
    	else
    	{
    		GetNextScreen();
    	}
    });

    It's auto assign event when partial page again generate.

    Wednesday, April 18, 2018 11:14 AM
  • User1568384800 posted

    Mgebhard, theres a lot of code associated with this which is why I've culled the amount I've posted. Through debugging I've found the event is no longer being switched off. 

    The Main menu page has a series of buttons - the main page has previous and next buttons at the bottom. The code looks for what the next button is in the main menu and loads that when the next button is pressed. On one page in particular, I decide I don't want it the next button to go to the next page in the series of buttons I want it to show a second page page (Theres a lot of questions on the page) so page 2 of 2. So when page one loads it unassigns the event from the new one, loads and hides what will be page 2 and assign a new event to the net button.

    I hope that makes sense.

    Wednesday, April 18, 2018 11:46 AM
  • User475983607 posted

    Mgebhard, theres a lot of code associated with this which is why I've culled the amount I've posted. Through debugging I've found the event is no longer being switched off. 

    The Main menu page has a series of buttons - the main page has previous and next buttons at the bottom. The code looks for what the next button is in the main menu and loads that when the next button is pressed. On one page in particular, I decide I don't want it the next button to go to the next page in the series of buttons I want it to show a second page page (Theres a lot of questions on the page) so page 2 of 2. So when page one loads it unassigns the event from the new one, loads and hides what will be page 2 and assign a new event to the net button.

    I hope that makes sense.

    It makes sense but we cannot debug code that we cannot see.  You'll need to use the debugging tools to find and fix the bug or post code that reproduces the issue.

    Why are you using JavaScript?  Is this an SPA?  

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 18, 2018 11:58 AM
  • User1568384800 posted

    Ok, I think I now know why this doesn't work. I have the buttons with the same IDs on each page, so it's started unassigning a button from the prior page still in the DOM.

    Wednesday, April 18, 2018 2:06 PM
  • User1568384800 posted

    Except it's emptied before the next page loads in. So maybe not.

    Wednesday, April 18, 2018 2:30 PM
  • User1568384800 posted

    You're right, I should have posted all or at least more of the code, as although I'd debugged it to that point, it was a bit of a red herring as it was the on ready event causing the problem for it. 

    Thanks for your time.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, April 18, 2018 3:56 PM