How to access newly created object's variable?












0















So I have this blueprint object:



function User (theName, theEmail) {
this.name = theName;
this.email = theEmail;
this.quizScores = ;
this.currentScore = 0;
}


I create a new user like this var user1 = new User (theName.value, theEmail.value); which is inside a function for event listener when user types in his name and email. Now there are questions and a button for next question. Everytime user clicks next question button, I want to increment currentScore by one. Problem is that it stays 0 all the time. I do it like this:



scoretag = document.createElement("p");
scoretag.innerHTML = user1.currentScore;
body.appendChild(scoretag);



Event listener and main loop:



for (var i = 0; i < theChoices[question1.theChoices].length; i++) {

var arrayQ = document.createElement("p");
arrayQ.innerHTML = theChoices[question1.theChoices][i];
list.appendChild(arrayQ);

var radio = document.createElement("input");
radio.type = "radio";
listOptions.appendChild(radio);

dbtn.addEventListener("click", function() {
//list.removeChild(arrayQ);
//listOptions.removeChild
list.removeChild(list.firstChild);
list.removeChild(list.lastChild);
user1.currentScore = user1.currentScore+1;
scoretag = document.createElement("p");
scoretag.innerHTML = user1.currentScore;
body.appendChild(scoretag);

})
}


UPDATE: I putted the code for appending a child to a body element inside the loop after incrementation of score, but this results in many numbers printed on the page one after another.



But like I said, when I try t increment by one on button click, it still keeps showing 0 on the screen. Any help?










share|improve this question

























  • show us the click listener

    – Bhojendra Rauniyar
    Nov 12 '18 at 22:50











  • Where do you actually increment the currentScore?

    – Mark Meyer
    Nov 12 '18 at 22:51











  • @BhojendraRauniyar updated

    – Limpuls
    Nov 12 '18 at 22:52











  • printed number of times because you're appending on each click.

    – Bhojendra Rauniyar
    Nov 12 '18 at 22:53











  • If I do it outside the loop it stays 0. What are your suggestions?

    – Limpuls
    Nov 12 '18 at 22:54
















0















So I have this blueprint object:



function User (theName, theEmail) {
this.name = theName;
this.email = theEmail;
this.quizScores = ;
this.currentScore = 0;
}


I create a new user like this var user1 = new User (theName.value, theEmail.value); which is inside a function for event listener when user types in his name and email. Now there are questions and a button for next question. Everytime user clicks next question button, I want to increment currentScore by one. Problem is that it stays 0 all the time. I do it like this:



scoretag = document.createElement("p");
scoretag.innerHTML = user1.currentScore;
body.appendChild(scoretag);



Event listener and main loop:



for (var i = 0; i < theChoices[question1.theChoices].length; i++) {

var arrayQ = document.createElement("p");
arrayQ.innerHTML = theChoices[question1.theChoices][i];
list.appendChild(arrayQ);

var radio = document.createElement("input");
radio.type = "radio";
listOptions.appendChild(radio);

dbtn.addEventListener("click", function() {
//list.removeChild(arrayQ);
//listOptions.removeChild
list.removeChild(list.firstChild);
list.removeChild(list.lastChild);
user1.currentScore = user1.currentScore+1;
scoretag = document.createElement("p");
scoretag.innerHTML = user1.currentScore;
body.appendChild(scoretag);

})
}


UPDATE: I putted the code for appending a child to a body element inside the loop after incrementation of score, but this results in many numbers printed on the page one after another.



But like I said, when I try t increment by one on button click, it still keeps showing 0 on the screen. Any help?










share|improve this question

























  • show us the click listener

    – Bhojendra Rauniyar
    Nov 12 '18 at 22:50











  • Where do you actually increment the currentScore?

    – Mark Meyer
    Nov 12 '18 at 22:51











  • @BhojendraRauniyar updated

    – Limpuls
    Nov 12 '18 at 22:52











  • printed number of times because you're appending on each click.

    – Bhojendra Rauniyar
    Nov 12 '18 at 22:53











  • If I do it outside the loop it stays 0. What are your suggestions?

    – Limpuls
    Nov 12 '18 at 22:54














0












0








0








So I have this blueprint object:



function User (theName, theEmail) {
this.name = theName;
this.email = theEmail;
this.quizScores = ;
this.currentScore = 0;
}


I create a new user like this var user1 = new User (theName.value, theEmail.value); which is inside a function for event listener when user types in his name and email. Now there are questions and a button for next question. Everytime user clicks next question button, I want to increment currentScore by one. Problem is that it stays 0 all the time. I do it like this:



scoretag = document.createElement("p");
scoretag.innerHTML = user1.currentScore;
body.appendChild(scoretag);



Event listener and main loop:



for (var i = 0; i < theChoices[question1.theChoices].length; i++) {

var arrayQ = document.createElement("p");
arrayQ.innerHTML = theChoices[question1.theChoices][i];
list.appendChild(arrayQ);

var radio = document.createElement("input");
radio.type = "radio";
listOptions.appendChild(radio);

dbtn.addEventListener("click", function() {
//list.removeChild(arrayQ);
//listOptions.removeChild
list.removeChild(list.firstChild);
list.removeChild(list.lastChild);
user1.currentScore = user1.currentScore+1;
scoretag = document.createElement("p");
scoretag.innerHTML = user1.currentScore;
body.appendChild(scoretag);

})
}


UPDATE: I putted the code for appending a child to a body element inside the loop after incrementation of score, but this results in many numbers printed on the page one after another.



But like I said, when I try t increment by one on button click, it still keeps showing 0 on the screen. Any help?










share|improve this question
















So I have this blueprint object:



function User (theName, theEmail) {
this.name = theName;
this.email = theEmail;
this.quizScores = ;
this.currentScore = 0;
}


I create a new user like this var user1 = new User (theName.value, theEmail.value); which is inside a function for event listener when user types in his name and email. Now there are questions and a button for next question. Everytime user clicks next question button, I want to increment currentScore by one. Problem is that it stays 0 all the time. I do it like this:



scoretag = document.createElement("p");
scoretag.innerHTML = user1.currentScore;
body.appendChild(scoretag);



Event listener and main loop:



for (var i = 0; i < theChoices[question1.theChoices].length; i++) {

var arrayQ = document.createElement("p");
arrayQ.innerHTML = theChoices[question1.theChoices][i];
list.appendChild(arrayQ);

var radio = document.createElement("input");
radio.type = "radio";
listOptions.appendChild(radio);

dbtn.addEventListener("click", function() {
//list.removeChild(arrayQ);
//listOptions.removeChild
list.removeChild(list.firstChild);
list.removeChild(list.lastChild);
user1.currentScore = user1.currentScore+1;
scoretag = document.createElement("p");
scoretag.innerHTML = user1.currentScore;
body.appendChild(scoretag);

})
}


UPDATE: I putted the code for appending a child to a body element inside the loop after incrementation of score, but this results in many numbers printed on the page one after another.



But like I said, when I try t increment by one on button click, it still keeps showing 0 on the screen. Any help?







javascript object this prototype






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 22:52







Limpuls

















asked Nov 12 '18 at 22:46









LimpulsLimpuls

337111




337111













  • show us the click listener

    – Bhojendra Rauniyar
    Nov 12 '18 at 22:50











  • Where do you actually increment the currentScore?

    – Mark Meyer
    Nov 12 '18 at 22:51











  • @BhojendraRauniyar updated

    – Limpuls
    Nov 12 '18 at 22:52











  • printed number of times because you're appending on each click.

    – Bhojendra Rauniyar
    Nov 12 '18 at 22:53











  • If I do it outside the loop it stays 0. What are your suggestions?

    – Limpuls
    Nov 12 '18 at 22:54



















  • show us the click listener

    – Bhojendra Rauniyar
    Nov 12 '18 at 22:50











  • Where do you actually increment the currentScore?

    – Mark Meyer
    Nov 12 '18 at 22:51











  • @BhojendraRauniyar updated

    – Limpuls
    Nov 12 '18 at 22:52











  • printed number of times because you're appending on each click.

    – Bhojendra Rauniyar
    Nov 12 '18 at 22:53











  • If I do it outside the loop it stays 0. What are your suggestions?

    – Limpuls
    Nov 12 '18 at 22:54

















show us the click listener

– Bhojendra Rauniyar
Nov 12 '18 at 22:50





show us the click listener

– Bhojendra Rauniyar
Nov 12 '18 at 22:50













Where do you actually increment the currentScore?

– Mark Meyer
Nov 12 '18 at 22:51





Where do you actually increment the currentScore?

– Mark Meyer
Nov 12 '18 at 22:51













@BhojendraRauniyar updated

– Limpuls
Nov 12 '18 at 22:52





@BhojendraRauniyar updated

– Limpuls
Nov 12 '18 at 22:52













printed number of times because you're appending on each click.

– Bhojendra Rauniyar
Nov 12 '18 at 22:53





printed number of times because you're appending on each click.

– Bhojendra Rauniyar
Nov 12 '18 at 22:53













If I do it outside the loop it stays 0. What are your suggestions?

– Limpuls
Nov 12 '18 at 22:54





If I do it outside the loop it stays 0. What are your suggestions?

– Limpuls
Nov 12 '18 at 22:54












1 Answer
1






active

oldest

votes


















1














Every time you increment the score, you need to update the HTML element. This is an idea:






function User (theName, theEmail) {
this.name = theName;
this.email = theEmail;
this.quizScores = ;
this.currentScore = 0;
}
var user1 = new User ("aa","bb");

function updateScore(){
user1.currentScore++;
document.getElementById('score').innerText = user1.currentScore;
}

<button id="btn" onclick="updateScore()">next</button>
<p id="score">0</p>








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%2f53271200%2fhow-to-access-newly-created-objects-variable%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Every time you increment the score, you need to update the HTML element. This is an idea:






    function User (theName, theEmail) {
    this.name = theName;
    this.email = theEmail;
    this.quizScores = ;
    this.currentScore = 0;
    }
    var user1 = new User ("aa","bb");

    function updateScore(){
    user1.currentScore++;
    document.getElementById('score').innerText = user1.currentScore;
    }

    <button id="btn" onclick="updateScore()">next</button>
    <p id="score">0</p>








    share|improve this answer






























      1














      Every time you increment the score, you need to update the HTML element. This is an idea:






      function User (theName, theEmail) {
      this.name = theName;
      this.email = theEmail;
      this.quizScores = ;
      this.currentScore = 0;
      }
      var user1 = new User ("aa","bb");

      function updateScore(){
      user1.currentScore++;
      document.getElementById('score').innerText = user1.currentScore;
      }

      <button id="btn" onclick="updateScore()">next</button>
      <p id="score">0</p>








      share|improve this answer




























        1












        1








        1







        Every time you increment the score, you need to update the HTML element. This is an idea:






        function User (theName, theEmail) {
        this.name = theName;
        this.email = theEmail;
        this.quizScores = ;
        this.currentScore = 0;
        }
        var user1 = new User ("aa","bb");

        function updateScore(){
        user1.currentScore++;
        document.getElementById('score').innerText = user1.currentScore;
        }

        <button id="btn" onclick="updateScore()">next</button>
        <p id="score">0</p>








        share|improve this answer















        Every time you increment the score, you need to update the HTML element. This is an idea:






        function User (theName, theEmail) {
        this.name = theName;
        this.email = theEmail;
        this.quizScores = ;
        this.currentScore = 0;
        }
        var user1 = new User ("aa","bb");

        function updateScore(){
        user1.currentScore++;
        document.getElementById('score').innerText = user1.currentScore;
        }

        <button id="btn" onclick="updateScore()">next</button>
        <p id="score">0</p>








        function User (theName, theEmail) {
        this.name = theName;
        this.email = theEmail;
        this.quizScores = ;
        this.currentScore = 0;
        }
        var user1 = new User ("aa","bb");

        function updateScore(){
        user1.currentScore++;
        document.getElementById('score').innerText = user1.currentScore;
        }

        <button id="btn" onclick="updateScore()">next</button>
        <p id="score">0</p>





        function User (theName, theEmail) {
        this.name = theName;
        this.email = theEmail;
        this.quizScores = ;
        this.currentScore = 0;
        }
        var user1 = new User ("aa","bb");

        function updateScore(){
        user1.currentScore++;
        document.getElementById('score').innerText = user1.currentScore;
        }

        <button id="btn" onclick="updateScore()">next</button>
        <p id="score">0</p>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 14:47

























        answered Nov 12 '18 at 23:03









        eag845eag845

        878611




        878611






























            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%2f53271200%2fhow-to-access-newly-created-objects-variable%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

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values