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 8234015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:20:06+00:00 2026-06-07T18:20:06+00:00

I am trying to use the response from a jQuery ajax request to set

  • 0

I am trying to use the response from a jQuery ajax request to set the innerHTML of a div with a certain id. This is the code I am using:

$(".show_comments").click(function(){


var articleName = $(this).closest("article").find(".articlename").attr('id')

$.ajax({
url: "commentView.php",
data: { 'articleName': articleName },
cache: false,
dataType: "html", // so that it will auto parse it as html
success: function(response){
$(this).closest("article").find(".comments").html(response);
}
}); 

however it doesn’t seem to be doing anything at all, I’ve tried googling around, but everything I can find says to do it the way I am doing it… I have tested in Firebug and the ajax request is giving me the exact response I want it too… But I just cant access this to set the innerHTML of the div to the response!

  • 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-07T18:20:08+00:00Added an answer on June 7, 2026 at 6:20 pm

    In your ajax success handler there is another scope and this points to not what you think. So change your code to:

    var articleName = $(this).closest("article").find(".articlename").attr('id'),
        that = this;
    
    $.ajax({
        url: "commentView.php",
        data: { 'articleName': articleName },
        cache: false,
        dataType: "html", // so that it will auto parse it as html
        success: function(response){
            $(that).closest("article").find(".comments").html(response);
        }
    }); 
    

    What I’ve changed: I added that variable that points to this instance and use it in the success handler instead.

    How would you debug it yourself: if you tried to output console.log(this) and console.log($(this).closest("article").find(".comments")); you would see that it returns a wrong object (in first case) and nothing in second.

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

Sidebar

Related Questions

I am trying to use the return value(response) from the callback outside the jQuery.post
EDITED I'm trying to use jquery/ajax to display data returned from a django method.
I'm trying to do an XMLHttpRequest from a local file (file://) using JQuery.ajax to
I am trying to use the jQuery $.post method to submit an ajax request
I'm trying to use Ajax together with jQuery to make a little window pop
I'm trying to use jQuery.getJSON to get data from a domain I don't own
I am trying to perform a simple jQuery AJAX attempt using either a .get()
Im ready to start pulling my hair out here. Trying to use jquery ajax
I am trying to read the response from an xhr request. I do not
I am using Codeigniter and trying to use the jQuery autocomplete with it. I

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.