-1

I am using telerik in an application I am creating, and i have a javascript on.click method which runs when an item is clicked in a telerik:RadContextMenu.

            <telerik:RadContextMenu ID="tkDocumentList" runat="server" OnClientItemClicking="DocumentListOnClientItemClicking">
                <Items>
                    <telerik:RadMenuItem Text="Edit Document" />
                    <telerik:RadMenuItem Text="Delete Document" />
                    <telerik:RadMenuItem Text="Upload Document" />
                    <telerik:RadMenuItem Text="Download Document" />
                    <telerik:RadMenuItem IsSeparator="True" />
                </Items>
            </telerik:RadContextMenu>

The function determines which item was checked and then executes some logic.

function DocumentListOnClientItemClicking(sender, args) {
    var index = args.get_item().get_index();
    if (index == 0) {
        console.log('Toggle in edit');
        $('.documentdetails>.modalContent').toggleClass('show');
        $('.showedit').val("true");
    }
    else if (index == 1) {
            var result = confirm("Are you sure you want to delete this item?");
            args.set_cancel(!result);
            sender.hide();
    }
    else if (index == 2) {
        console.log('Toggle in upload');
        $('.documentdetails>.modalContent').toggleClass('show');
    }
}

My problem is that when the index is 0 it goes into the if and else if, ill try to explain my problem. Imagine function DocumentListOnClientItemClicking(sender, args) { is line:0 and the final ending } is line: 16. Here is how this method executes:

  1. 1 (Index is 0)
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7 (index is not 1)
  8. 11
  9. 12 (index is not 2, however it runs part of this else if)
  10. 14 (doesnt hit console log and break on this line)

Does anyone know why this might be happening?

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Srb1313711
  • 2,017
  • 5
  • 24
  • 35
  • 3
    *"Here is how this method executes:"* No, it isn't. `index` cannot be simultaneously `== 0` and `== 1`. Something else happened, not that. [Create an MCVE](/help/mcve) and post it as a Stack Snippet (the `<>` button) in the question. Odds are very high that while doing that, you'll figure out what's wrong; if not, the snippet will let us help you. – T.J. Crowder Nov 13 '15 at 10:54
  • Check that event isnt fired multiple times in a row with different index. Btw, what do you mean by console doesnt hit and break? Im not sure then your observation is correct – A. Wolff Nov 13 '15 at 10:56
  • @T.J.Crowder im not sure how much more information I can give, im as confused as you are, what else would be helpful for you to know? – Srb1313711 Nov 13 '15 at 11:12
  • @A.Wolff i have checked and have added a watch to the index, it is definitely 0 even when it goes into that second else if – Srb1313711 Nov 13 '15 at 11:13
  • @Srb1313711: You have to let go of that: It definitely **is not** `0` when it goes into that `else if`. It just isn't. [`select` isn't broken](https://pragprog.com/the-pragmatic-programmer/extracts/tips). Once you let go of the belief that that's what's happening, you open your mind to what's actually happening, whatever that is. Re what more we need to know, I said that: You need to build an [MCVE](/help/mcve). – T.J. Crowder Nov 13 '15 at 11:14
  • @T.J.Crowder I understand what you are saying, im still trying to find the issue but my JS knowledge isnt the best which is why i've tried to find help on here – Srb1313711 Nov 13 '15 at 11:15
  • `else if (index == 2) { console.log(index == 0); }` For sure doesn't output `true` – A. Wolff Nov 13 '15 at 11:25
  • @A.Wolff it doesnt output anything. I changed the 2 to a 3 and it still gets into this else, itll only hit the last line, and it seems to break but i cant see any error – Srb1313711 Nov 13 '15 at 11:34
  • @Srb1313711 So how do you debug it? Why do you think it goes in none relevant `else if` block? Provide MCVE, stop letting us guessing... `however it runs part of this else if` It doesn't run just some part of `else if` block, your observation is wrong. If you mean just the toggleClass is done, well, you are toggling it in `if (index == 0)` too, maybe you are binding event twice, don't know... – A. Wolff Nov 13 '15 at 11:37
  • @A.Wolff hi sorry im not sure what an MCVE is, I read the provided link and thought id fulled the criteria. I.e. describing my problem, providing code im not sure what else you want – Srb1313711 Nov 13 '15 at 11:40
  • @Srb1313711 A sample replicating issue... Because your issue isn't possible regarding your posted code. FYI, you should read: http://stackoverflow.com/questions/988363/how-can-i-debug-my-javascript-code – A. Wolff Nov 13 '15 at 11:40
  • @A.Wolff thank you I am able to debug it in the browser – Srb1313711 Nov 13 '15 at 11:42

1 Answers1

0

This hasnt completely resolved my issue, but the telerik context menu was in a updatepanel and click one of the items posts the page back. Preventing these from posting back fixed this issue but caused others in my particular app. I have no idea why this causes the issue but its the best i could come up with.

Srb1313711
  • 2,017
  • 5
  • 24
  • 35