0

I want to pass value of inner function to outer function. I want to change value of a=2 when treeImage is clicked. Here is my code, what I am doing wrong?

 <script type="text/javascript" language="javascript">   
     Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
     function EndRequestHandler(sender, args) {
         var a;
         var treeViewData = window["<%=TreeView2.ClientID%>" + "_Data"];
         var treeView1 = $('#<%= TreeView2.ClientID %>');
         var treeNodes = treeView1.find("div[id$=Nodes]");
         var treeImages = treeView1.find("img").not("img[alt=\'\']");
         treeImages.click(function () {//if image is clicked a should be 2
             a = 2;
         });
         if (treeViewData.selectedNodeID.value != ""&&a!=2)
         {
             $find("dde").hide();
         }
         else
         {
             $find("dde").show();
         }  
     }
</script>
Firdavs Kurbonov
  • 1,252
  • 4
  • 16
  • 42
  • 1
    The `click` handler happens when the user clicks the element, right? So it should be pretty clear that the code that is after you bind the `click` handler will run *long* before a click takes place. If you want that code to run on every click, then put it *inside* the `click` handler. –  Oct 24 '14 at 18:13
  • 1
    Short answer is that you can't, currently. The assignment of `a` is bound to an event, `click`, and the `if` will be executed before that event has actually occurred. Any uses of `a` that depend on the assignment will have to also be bound to it. – Jonathan Lonowski Oct 24 '14 at 18:14
  • @squint When I put if inside click event it does not hide or show dde, that is why I wanted to manipulate it with variable. – Firdavs Kurbonov Oct 24 '14 at 18:24
  • Then you should be asking about the actual problem and not what you assumed to be an appropriate solution. [What is the XY Problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) –  Oct 24 '14 at 18:26

1 Answers1

-2

Put var a outside EndRequestHandler().

What you're doing in your code is a closure, but it's a pointless closure at that. The function for treeImages.click is "closing around" the a variable, but given your code it doesn't really act any different than if var a were inside the treeImages.click function.

kevin628
  • 3,458
  • 3
  • 30
  • 44
  • Thanks for the ninja downvotes. I'd appreciate comments. – kevin628 Oct 24 '14 at 18:15
  • I'd think you'd take a closer look at the question after getting 2 DVs. He wants the variable assigned in the inner function *(the click handler)* to be accessible to the outer function *(the `EndRequestHandler`)*, and it already is. The issue isn't one of scope, it's one of availability of the data for the purpose shown in that function. `if (treeViewData.selectedNodeID.value != ""&&a!=2)` –  Oct 24 '14 at 18:18
  • The votes are likely because the issue isn't really with the scope of the `a` variable, or where it can be referenced. It's within reach of everything inside `EndRequestHandler`, including embedded `function`s that don't `var` declare their own `a`. The issue is instead with the order that the code executes, which isn't strictly top-to-bottom. The `click()` handler that assigns `a` will occur *sometime later*, well after the `if` has been evaluated. – Jonathan Lonowski Oct 24 '14 at 18:21
  • Yep, both of you are correct. I was offering a solution to _his_ code to achieve _his_ desired goal. Move the variable initialization to a global scope and the problem is solved for _his_ code. Is it the way I'd do it? Absolutely not. Will the code have vague side effects that will make debugging hard? Absolutely yes. – kevin628 Oct 24 '14 at 18:24
  • In this code I want to show DropDownExtender if plus(minus) sign is clicked in Treeview. If text is clicked it should hide. – Firdavs Kurbonov Oct 24 '14 at 18:26
  • @kevin628 I already tried it, it does not help – Firdavs Kurbonov Oct 24 '14 at 18:27
  • @kevin628: Except that your answer is not a solution to *his* code. It'll make no difference. –  Oct 24 '14 at 18:28
  • @squint If I may offer a suggestion for you in life-- when someone is wrong, stay objective by solving the issue at hand and not by trying to insult a person for simply being wrong. The condescending swipe in your comments do not promote a healthy environment of conversation, let alone problem solving. Best of luck, mate. – kevin628 Oct 24 '14 at 18:31
  • And in turn I'll offer you a suggestion. If you're hyper-sensitive to criticism, don't ask for a reason for down votes with sarcasm. You asked why you were down voted and I've told you what the problem is in detail. Your answer is incorrect. What more do you want? A hug? –  Oct 24 '14 at 18:35
  • No, I want you to remove your attitude and your personal bias. If you're going to offer help, then offer it with _kindness_. What happened to common courtesy? Now, the reason why I asked for comments is because this website is notorious for its ninja down voters, and SO does not require people to enter comments before a down vote is approved. No change is effective when people disapprove but never communicate why. – kevin628 Oct 24 '14 at 18:38
  • If you're going to request that someone take the time to explain why you were down voted, then request it with kindness. What happened to common courtesy? Now, the reason why I told you that I'd think you'd take a closer look at the question is because this website is notorious for ninja answers, and SO does not require people fully read and comprehend the question before entering an answer. No answer is effective when people don't take the time to understand what is being asked. –  Oct 24 '14 at 18:45