2

I have an html button element that has an attribute onclick="onprof()" which correctly gets called. The button is above the <script> element which is the last thing before the </body> tag. I set a breakpoint at the line that contains the code var product = [] which gets activated on the page reload. I then hit the button which calls the function (I also have a breakpoint there) and that works fine, but then it goes to my original breakpoint and ends up breaking the code because it reinitializes the value of my variable product. Why is that Javascript being called again and how can I not get it to be called?

       <script type="text/javascript">

            var product = [];
            var temp=[]; 
            temp.push(1);
            temp.push("PET");
            product.push(temp);
            temp = [];
            temp2 = [];
            function onprof()
            {
               //alert(product);
                document.getElementById("typeform").innerHTML = product.toString();
            }
            function onprod()
            {

            }
            function onser()
            {

            }

        </script>
Matthew
  • 3,886
  • 7
  • 47
  • 84
  • 2
    Is the ` – Phil Feb 19 '18 at 04:04
  • It shouldn't call that code again. Only thing I can think of is that the page is reloading. – Barmar Feb 19 '18 at 04:04
  • Why are you seeing that makes you think product is getting reinitialized? – EmptyArsenal Feb 19 '18 at 04:04
  • @EmptyArsenal OP says they hit the breakpoint on the first line again in their debugger. – Phil Feb 19 '18 at 04:06
  • @Phil I feel like upvoting just because he knows how to use the debugger. – Barmar Feb 19 '18 at 04:07
  • @Phil is that an attack on me? Do you think that is necessary? – Matthew Feb 19 '18 at 04:08
  • @Barmar I know, right! – Phil Feb 19 '18 at 04:09
  • @Matthew Not sure what you mean by _"attack"_ or in reference to which comment. I was explaining to another user why it's clear that you're seeing the array re-initialised (ie, the debugger does not lie) – Phil Feb 19 '18 at 04:10
  • I was just wondering why you said "I feel like upvoting just because he knows how to use the debugger." It has a tone of "this guy has no clue what is going on." – Matthew Feb 19 '18 at 04:12
  • 1
    @Matthew no, Barmar (not me) is praising you for actually using a debugger which is a rare occurrence on this site – Phil Feb 19 '18 at 04:12
  • Okay, sorry about being insensitive... I am arguing with people on other non-programming sites, and I think my attitude carried over to this. Thank you for the clarification. – Matthew Feb 19 '18 at 04:14

1 Answers1

2

Looks like you have a form which contains the button. On submit, forms reload the page. You can disable this behaviour by either:

  1. Set the button type as type="button". By default, it is submit.

or

  1. Return false from your onprof function and use onclick="return onprof()".
Phil
  • 157,677
  • 23
  • 242
  • 245
agarwalankur85
  • 274
  • 2
  • 9
  • The type=button worked perfectly! Thank you. I tried just returning false too and it didn't seem to work, but maybe I wasn't doing something right, but it is all working now. – Matthew Feb 19 '18 at 04:15
  • 1
    @Phil - Thanks for the edit. I am bit new to SO. Next time, I will try to clarify assumptions before posting answer. Cheers. – agarwalankur85 Feb 19 '18 at 04:17
  • 1
    @Matthew - Checkout the edit from @Phil. You need to return from `onclick` too. – agarwalankur85 Feb 19 '18 at 04:18