Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8494845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T23:24:33+00:00 2026-06-10T23:24:33+00:00

I am new to javascript and I would like to know how I can

  • 0

I am new to javascript and I would like to know how I can access my class variable inside my ajax callback

success: function(data) {
            var obj = jQuery.parseJSON(data);
            this.baralho = obj.countBaralho;

My Class

var Placar = {
    descarte : 0,
    jogador1: 0,
    jogador2: 0,
    baralho: 0,

    fetchFromServer: function(){
        $.ajax({
            url: baseUrl+'get-placar',
            success: function(data) {
                var obj = jQuery.parseJSON(data);
                this.baralho = obj.countBaralho;
                this.descarte = obj.countDescarte;
                this.jogador1 = obj.numCartas1;
                this.jogador2 = obj.numCartas2;
            }
        });

    },

    update: function() {
        $('#num-cartas-1').html(this.jogador1);
        $('#num-cartas-2').html(this.jogador2);
        $('#cartas-restantes').html(this.baralho);
        $('#num-cartas-descarte').html(this.descarte);
    }

}

The values aren’t being setted from the server, How can I fix it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T23:24:34+00:00Added an answer on June 10, 2026 at 11:24 pm

    The this in the ajax’s success function is not your object(its probably an XMLHttpRequest object).

    I would use a constructor to create the object instead of an object literal, and create a closure to be able to access the object in any context.

    var Placar = new (function(){
        this.descarte = 0;
        this.jogador1 = 0;
        this.jogador2 = 0;
        this.baralho = 0;
                var self = this;        
        this.fetchFromServer = function(){
            $.ajax({
                url: baseUrl+'get-placar',
                success: function(data) {
                    var obj = jQuery.parseJSON(data);
                    self.baralho = obj.countBaralho;
                    self.descarte = obj.countDescarte;
                    self.jogador1 = obj.numCartas1;
                    self.jogador2 = obj.numCartas2;
                }
            });
    
        };
    
        this.update = function() {
            $('#num-cartas-1').html(self.jogador1);
            $('#num-cartas-2').html(self.jogador2);
            $('#cartas-restantes').html(self.baralho);
            $('#num-cartas-descarte').html(self.descarte);
        };
    
    })();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm absolutely new to JavaScript and would like to modify a textarea of a
I'm still new to Javascript and would like to create a 'pages' feature for
I am new to Javascript and object-oriented programming in general. I would like to
I would like to define the following in Javascript: var tileXml = new Array();
would like to know how can this be implemented in Joomla. I have a
I would like to know how I can change the date in my selectedDate
i'm new to javascript, and I would like to retrieve values from JSON and
I'm using jquerys slideToggle method and would like to add a callback function that
I am new to Javascript & relatively new to HTML. I would like to
I am new to MS Dynamics and I would like to know if there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.