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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:56:10+00:00 2026-06-03T15:56:10+00:00

I have the following code which works: $(document).ready(function() { var xhr = false; func_two();

  • 0

I have the following code which works:

$(document).ready(function() {
    var xhr = false;

    func_two();

    $('.button_one').click( function(event) {
        event.preventDefault(); 

        if ($("#textbox_one").val().length > 0) {
            func_one( $("#textbox_one").val() );
        } else {
            func_two();
        }
    });

    function func_one( phrase ) {
        xhr = $.ajax({
            type: 'POST',
            url: 'url here',
            data: '{phrase: "' + phrase + '"}',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function( data ) {
                $("#inner_div").empty();

                if( data.d[0] ) {
                    $.each( data.d, function( index, row ) {
                        $("#inner_div").append( "dynamic data goes here" );
                    });
                }
            }
        });
    }

    function func_two() {
        $('#inner_div').empty().append( "static data goes here" );
    }

});

This works fine as I am directly specifying which tags cause an effect and which tags get affected. The problem is that these div’s are infact widgets or gadgets, and I need to allow the user to have more than 1 of these widgets or gadgets on the screen at at any given time. Things seem to go totally wrong when I try this because “I assume” of the use of an id for the controls and divs.

So, I have tried using classes instead, which seem to work in the test code here:

$(this).closest('.main_div').find('.sub-div').text("data goes here");

However, when I try to apply this code to the original code above, it does not seem to work. It gives me an Cannot call method 'closest' of undefined according to Chromium’s Developer tools.

The code which I have tried which does not seem to work looks like this:

$(document).ready(function() {
    var xhr = false;

    func_two();

    $('.button_one').click( function(event) {
        event.preventDefault(); 

        if ($("#textbox_one").val().length > 0) {
            func_one( $(this), $("#textbox_one").val() );
        } else {
            func_two( $(this) );
        }
    });

    function func_one( that, phrase ) {
        xhr = $.ajax({
            type: 'POST',
            url: 'url here',
            data: '{phrase: "' + phrase + '"}',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function( data ) {
                $("#inner_div").empty();
                that.closest('.outer_div').find('.inner_div').empty();

                if( data.d[0] ) {
                    $.each( data.d, function( index, row ) {
                        that.closest('.outer_div').find('.inner_div').append( row.column );
                    });
                }
            }
        });
    }

    function func_two( that ) {
    that.closest('.outer_div').find('.inner_div').append( "static data goes here" );
    }

});

The HTML looks like this

<div class="outer_div">

    <div class="inner_div">
        results go here
    </div>

    <div class="footer_div">
        <input type="button" class="button_one" />
    </div>

</div>

Can someone please help me implement the test code into the original code?

  • 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-03T15:56:12+00:00Added an answer on June 3, 2026 at 3:56 pm

    Try this out, I believe your button is being deleted, and there’s no longer a reference to that element. By storing a reference to the targetted sub div, THAT stays in the context of what you’re trying to do 🙂

    function func_one( that ) {
        xhr = $.ajax({
            type: 'POST',
            url: 'some url here',
            data: 'data to post here',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function( data ) {
                var column_elements = [];
    
                for(var i = 0; i < data.d.length; i++) {
                    column_elements.push(data.d[i].column);
                }
    
                $('.sub_div', that.parents('.main_div:first')).text(column_elements.join(", "));
            }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following jQuery code, which works fine: $(document).ready(function () { $('#link1').click(function ()
I have the following code which works great:- $('.ajax a').click(function() { getpageajax(this.href); return false;
I have the following block of code which works fine; var boughtItemsToday = (from
I have the following jquery code: $(document).ready(function() { $(#build_table, a.coursename, .Start Date, .Book Title,
I have following code which works for radio buttons but need to be changed
I have the following code which works just fine when the method is POST,
I have the following code which works perfectly, but I want to allow access
I have the following code which works when i put it in any blank
At the moment I have the following code which works fine. label = new
I have trouble to get gluLookAt working. I have the following code which works

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.