How to increment counter value inside an onClick event? The Next CEO of Stack OverflowHow can I get query string values in JavaScript?How to get the value from the GET parameters?How can I add a key/value pair to a JavaScript object?How to remove item from array by value?How to access the correct `this` inside a callback?React js onClick can't pass value to methodReturn value from javascript onclick to variable or change global variable with onclickOnclick button that add random value from array in textarea no refreshJavascript function counter add up despite declaring new var every timeButton onclick event change all the select option selection in a row

Is a distribution that is normal, but highly skewed considered Gaussian?

Is the D&D universe the same as the Forgotten Realms universe?

Why does standard notation not preserve intervals (visually)

What steps are necessary to read a Modern SSD in Medieval Europe?

Why didn't Khan get resurrected in the Genesis Explosion?

Won the lottery - how do I keep the money?

Where do students learn to solve polynomial equations these days?

INSERT to a table from a database to other (same SQL Server) using Dynamic SQL

What was the first Unix version to run on a microcomputer?

Flying from Cape Town to England and return to another province

Why isn't the Mueller report being released completely and unredacted?

Would this house-rule that treats advantage as a +1 to the roll instead (and disadvantage as -1) and allows them to stack be balanced?

Do I need to write [sic] when a number is less than 10 but isn't written out?

How do I align (1) and (2)?

0 rank tensor vs 1D vector

Proper way to express "He disappeared them"

Method for adding error messages to a dictionary given a key

A Man With a Stainless Steel Endoskeleton (like The Terminator) Fighting Cloaked Aliens Only He Can See

Would a grinding machine be a simple and workable propulsion system for an interplanetary spacecraft?

How to count occurrences of text in a file?

Why isn't acceleration always zero whenever velocity is zero, such as the moment a ball bounces off a wall?

Can a Bladesinger Wizard use Bladesong with a Hand Crossbow?

Why did CATV standarize in 75 ohms and everyone else in 50?

Is it convenient to ask the journal's editor for two additional days to complete a review?



How to increment counter value inside an onClick event?



The Next CEO of Stack OverflowHow can I get query string values in JavaScript?How to get the value from the GET parameters?How can I add a key/value pair to a JavaScript object?How to remove item from array by value?How to access the correct `this` inside a callback?React js onClick can't pass value to methodReturn value from javascript onclick to variable or change global variable with onclickOnclick button that add random value from array in textarea no refreshJavascript function counter add up despite declaring new var every timeButton onclick event change all the select option selection in a row










0















I'm quite new to Google App Maker and programming in general, I'm making an app where when a "+" button is clicked (onClick event) fields are added (kinda like a new row in a table with every click). I'm trying to make a counter to keep track of how many "rows" are being created. The thing is that since in App Maker all the code apparently has to be made inside the onClick event, i can't set a global variable and every time the button is clicked the counter restarts, therefore always being 1.



I tried with localStorage but i don't exactly know how to make it work, since it still will always save the same value. How can i fix this?



 var count = 0;
count = count +1;
localStorage.setItem('counter', JSON.stringify(parseFloat(count)));

textArea.className = 'app-TextArea';
textArea.style.margin = '8px';
textArea.setAttribute("placeholder", "Follow up # " + localStorage.getItem('counter');

widget.root.descendants.Panel1.getElement().appendChild(textArea);









share|improve this question

















  • 1





    If this is an onClick event can you post the surrounding code please.

    – James
    Mar 21 at 17:31











  • That's the thing, in App Maker it doesn't seem like i can add surrounding code. It just gives the option to add an onClick event and add the code there, hence i cant initialize "count" outside the onClick event.

    – J. IT
    Mar 21 at 17:36















0















I'm quite new to Google App Maker and programming in general, I'm making an app where when a "+" button is clicked (onClick event) fields are added (kinda like a new row in a table with every click). I'm trying to make a counter to keep track of how many "rows" are being created. The thing is that since in App Maker all the code apparently has to be made inside the onClick event, i can't set a global variable and every time the button is clicked the counter restarts, therefore always being 1.



I tried with localStorage but i don't exactly know how to make it work, since it still will always save the same value. How can i fix this?



 var count = 0;
count = count +1;
localStorage.setItem('counter', JSON.stringify(parseFloat(count)));

textArea.className = 'app-TextArea';
textArea.style.margin = '8px';
textArea.setAttribute("placeholder", "Follow up # " + localStorage.getItem('counter');

widget.root.descendants.Panel1.getElement().appendChild(textArea);









share|improve this question

















  • 1





    If this is an onClick event can you post the surrounding code please.

    – James
    Mar 21 at 17:31











  • That's the thing, in App Maker it doesn't seem like i can add surrounding code. It just gives the option to add an onClick event and add the code there, hence i cant initialize "count" outside the onClick event.

    – J. IT
    Mar 21 at 17:36













0












0








0








I'm quite new to Google App Maker and programming in general, I'm making an app where when a "+" button is clicked (onClick event) fields are added (kinda like a new row in a table with every click). I'm trying to make a counter to keep track of how many "rows" are being created. The thing is that since in App Maker all the code apparently has to be made inside the onClick event, i can't set a global variable and every time the button is clicked the counter restarts, therefore always being 1.



I tried with localStorage but i don't exactly know how to make it work, since it still will always save the same value. How can i fix this?



 var count = 0;
count = count +1;
localStorage.setItem('counter', JSON.stringify(parseFloat(count)));

textArea.className = 'app-TextArea';
textArea.style.margin = '8px';
textArea.setAttribute("placeholder", "Follow up # " + localStorage.getItem('counter');

widget.root.descendants.Panel1.getElement().appendChild(textArea);









share|improve this question














I'm quite new to Google App Maker and programming in general, I'm making an app where when a "+" button is clicked (onClick event) fields are added (kinda like a new row in a table with every click). I'm trying to make a counter to keep track of how many "rows" are being created. The thing is that since in App Maker all the code apparently has to be made inside the onClick event, i can't set a global variable and every time the button is clicked the counter restarts, therefore always being 1.



I tried with localStorage but i don't exactly know how to make it work, since it still will always save the same value. How can i fix this?



 var count = 0;
count = count +1;
localStorage.setItem('counter', JSON.stringify(parseFloat(count)));

textArea.className = 'app-TextArea';
textArea.style.margin = '8px';
textArea.setAttribute("placeholder", "Follow up # " + localStorage.getItem('counter');

widget.root.descendants.Panel1.getElement().appendChild(textArea);






javascript counter google-app-maker






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 21 at 17:25









J. ITJ. IT

52




52







  • 1





    If this is an onClick event can you post the surrounding code please.

    – James
    Mar 21 at 17:31











  • That's the thing, in App Maker it doesn't seem like i can add surrounding code. It just gives the option to add an onClick event and add the code there, hence i cant initialize "count" outside the onClick event.

    – J. IT
    Mar 21 at 17:36












  • 1





    If this is an onClick event can you post the surrounding code please.

    – James
    Mar 21 at 17:31











  • That's the thing, in App Maker it doesn't seem like i can add surrounding code. It just gives the option to add an onClick event and add the code there, hence i cant initialize "count" outside the onClick event.

    – J. IT
    Mar 21 at 17:36







1




1





If this is an onClick event can you post the surrounding code please.

– James
Mar 21 at 17:31





If this is an onClick event can you post the surrounding code please.

– James
Mar 21 at 17:31













That's the thing, in App Maker it doesn't seem like i can add surrounding code. It just gives the option to add an onClick event and add the code there, hence i cant initialize "count" outside the onClick event.

– J. IT
Mar 21 at 17:36





That's the thing, in App Maker it doesn't seem like i can add surrounding code. It just gives the option to add an onClick event and add the code there, hence i cant initialize "count" outside the onClick event.

– J. IT
Mar 21 at 17:36












2 Answers
2






active

oldest

votes


















0














You should get data from localStorage first, then increment it and finally set the new value the localStorage.



var count = parseInt(localStorage('counter')) || 0; // Get value from localStorage 
count = count + 1;
localStorage.setItem('counter', count); // Set new value to localStorage

textArea.className = 'app-TextArea';
textArea.style.margin = '8px';
textArea.setAttribute("placeholder", "Follow up # " + count); // Use new value

widget.root.descendants.Panel1.getElement().appendChild(textArea);





share|improve this answer























  • Thank you! It worked!

    – J. IT
    Mar 21 at 19:30


















0














You can use the page custom properties.



So in the the page, you can a Number custom property and name it counter.
Then your code should look like this:



 var count = widget.root.properties.counter || 0;
count += 1;
widget.root.properties.counter = count;

textArea.className = 'app-TextArea';
textArea.style.margin = '8px';
textArea.setAttribute("placeholder", "Follow up # " + count);

widget.root.descendants.Panel1.getElement().appendChild(textArea);





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55286040%2fhow-to-increment-counter-value-inside-an-onclick-event%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You should get data from localStorage first, then increment it and finally set the new value the localStorage.



    var count = parseInt(localStorage('counter')) || 0; // Get value from localStorage 
    count = count + 1;
    localStorage.setItem('counter', count); // Set new value to localStorage

    textArea.className = 'app-TextArea';
    textArea.style.margin = '8px';
    textArea.setAttribute("placeholder", "Follow up # " + count); // Use new value

    widget.root.descendants.Panel1.getElement().appendChild(textArea);





    share|improve this answer























    • Thank you! It worked!

      – J. IT
      Mar 21 at 19:30















    0














    You should get data from localStorage first, then increment it and finally set the new value the localStorage.



    var count = parseInt(localStorage('counter')) || 0; // Get value from localStorage 
    count = count + 1;
    localStorage.setItem('counter', count); // Set new value to localStorage

    textArea.className = 'app-TextArea';
    textArea.style.margin = '8px';
    textArea.setAttribute("placeholder", "Follow up # " + count); // Use new value

    widget.root.descendants.Panel1.getElement().appendChild(textArea);





    share|improve this answer























    • Thank you! It worked!

      – J. IT
      Mar 21 at 19:30













    0












    0








    0







    You should get data from localStorage first, then increment it and finally set the new value the localStorage.



    var count = parseInt(localStorage('counter')) || 0; // Get value from localStorage 
    count = count + 1;
    localStorage.setItem('counter', count); // Set new value to localStorage

    textArea.className = 'app-TextArea';
    textArea.style.margin = '8px';
    textArea.setAttribute("placeholder", "Follow up # " + count); // Use new value

    widget.root.descendants.Panel1.getElement().appendChild(textArea);





    share|improve this answer













    You should get data from localStorage first, then increment it and finally set the new value the localStorage.



    var count = parseInt(localStorage('counter')) || 0; // Get value from localStorage 
    count = count + 1;
    localStorage.setItem('counter', count); // Set new value to localStorage

    textArea.className = 'app-TextArea';
    textArea.style.margin = '8px';
    textArea.setAttribute("placeholder", "Follow up # " + count); // Use new value

    widget.root.descendants.Panel1.getElement().appendChild(textArea);






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 21 at 18:02









    birdbird

    1,088620




    1,088620












    • Thank you! It worked!

      – J. IT
      Mar 21 at 19:30

















    • Thank you! It worked!

      – J. IT
      Mar 21 at 19:30
















    Thank you! It worked!

    – J. IT
    Mar 21 at 19:30





    Thank you! It worked!

    – J. IT
    Mar 21 at 19:30













    0














    You can use the page custom properties.



    So in the the page, you can a Number custom property and name it counter.
    Then your code should look like this:



     var count = widget.root.properties.counter || 0;
    count += 1;
    widget.root.properties.counter = count;

    textArea.className = 'app-TextArea';
    textArea.style.margin = '8px';
    textArea.setAttribute("placeholder", "Follow up # " + count);

    widget.root.descendants.Panel1.getElement().appendChild(textArea);





    share|improve this answer



























      0














      You can use the page custom properties.



      So in the the page, you can a Number custom property and name it counter.
      Then your code should look like this:



       var count = widget.root.properties.counter || 0;
      count += 1;
      widget.root.properties.counter = count;

      textArea.className = 'app-TextArea';
      textArea.style.margin = '8px';
      textArea.setAttribute("placeholder", "Follow up # " + count);

      widget.root.descendants.Panel1.getElement().appendChild(textArea);





      share|improve this answer

























        0












        0








        0







        You can use the page custom properties.



        So in the the page, you can a Number custom property and name it counter.
        Then your code should look like this:



         var count = widget.root.properties.counter || 0;
        count += 1;
        widget.root.properties.counter = count;

        textArea.className = 'app-TextArea';
        textArea.style.margin = '8px';
        textArea.setAttribute("placeholder", "Follow up # " + count);

        widget.root.descendants.Panel1.getElement().appendChild(textArea);





        share|improve this answer













        You can use the page custom properties.



        So in the the page, you can a Number custom property and name it counter.
        Then your code should look like this:



         var count = widget.root.properties.counter || 0;
        count += 1;
        widget.root.properties.counter = count;

        textArea.className = 'app-TextArea';
        textArea.style.margin = '8px';
        textArea.setAttribute("placeholder", "Follow up # " + count);

        widget.root.descendants.Panel1.getElement().appendChild(textArea);






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 21 at 19:33









        MorfinismoMorfinismo

        2,5401820




        2,5401820



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55286040%2fhow-to-increment-counter-value-inside-an-onclick-event%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript