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

  • SEARCH
  • Home
  • 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 8168707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T20:36:55+00:00 2026-06-06T20:36:55+00:00

I think I am having a problem with the scope of ‘this’ when needing

  • 0

I think I am having a problem with the scope of ‘this’ when needing to do a simple css change on an element based on an Ajax return.

$('.time_slot_holder').click(function(){
    var data_day=$(this).data('agent_day');
    var data_time=$(this).data('agent_time');
    var data="agent_id="+agent_id+"&day="+data_day+"&time="+data_time
    $.ajax({
        type:"POST",
        url:"admin_includes/book_time.php",
        data:data,
        success:function(html){
            var split_html=html.split("|")
            if(split_html[0]=="B"){
                //booking exists
                alert("Bookings for this time slot exist. Contact Agent to arrange a re-assignment of this appointmnet.");
            }
            if(split_html[0]=="C"){
                //added to db
                $(this, '.time_slot_holder').css('background-color', 'red');
            }
            if(split_html[0]=="D"){

            }
        }
    });//end ajax
});

All I am trying to do is change the color on a grid with elements .time_slot_holder

Ignore the clumsy split callback from the ajax request this was just an attempt to identify the element via data attributes. Basically I need to get the reference of the clicked element through to the callback of the ajax.

  • 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-06T20:36:57+00:00Added an answer on June 6, 2026 at 8:36 pm

    You should set the context option in the AJAX request to this;

    $.ajax({
        type:"POST",
        url:"admin_includes/book_time.php",
        context: this,
        //..
    });
    

    Otherwise, this ends up being the jqXHR object.

    Another (more common) approach is to store the value of this in another variable;

    var that = this;
    
    $.ajax({
        type:"POST",
        url:"admin_includes/book_time.php",
        data:data,
        success:function(html){
            // Use `that` instead of `this` in here.
        }
    });//end ajax
    

    Also, I’m not sure what $(this, '.time_slot_holder') is supposed to be selecting, but I’m not sure it’ll work (need to see your HTML markup).

    If this is a descendant of '.time_slot_holder', then it’ll work fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think I'm having a problem with the this element in the code below
This is related to this post . I think I am having problem with
I am having what I think is a simple problem chaining two scopes (Rails
I'm having a real problem with JavaScript scope in IE 9. This is inside
I'm having a problem with Fluent NHibernate mappings, I think, and can't quite get
I'm having a problem with logic programming, but I think that could be another
I have been having the following problem, i think it's probably due to the
I think I might be having a stack overflow problem or something similar in
Having problems iterating. Problem has to do with const correctness, I think. I assume
I'm having lots of problem aligning an image on CSS. Here is the thing,

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.