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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:20:20+00:00 2026-06-09T11:20:20+00:00

I am trying to submit the ID of the parent DIV that a button

  • 0

I am trying to submit the ID of the parent DIV that a button is in using AJAX. This is the markup:

<div id="32"><input id="button" type="button" class="before" onclick="add_fav();" /></div>

and this is the ajax script in its current state:

<script>
function add_fav()
{   
jQuery.ajax({       
      url: 'addfav.php',
      type: 'POST',
      data: {'id':jQuery(this).closest("div").attr("id"),is_ajax: 1},
      success: function(html) {
        jQuery('#button').removeClass('before');
        jQuery('#button').addClass('after');
        jQuery('#button').attr('disabled', 'disabled');
      }, 
      error: function() {
        jQuery('#error').html('<div>Error! Unable to add food item.</div>');
      }
    });
}
</script>

This isn’t working so the method I am using to pass the id of the DIV is clearly not right. Perhaps someone could put me straight?

  • 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-09T11:20:22+00:00Added an answer on June 9, 2026 at 11:20 am

    If using an inline click handler pass a reference to the clicked item to your function:

    onclick="add_fav(this);"
    

    Then:

    function add_fav(btn)
    {   
       jQuery.ajax({       
          url: 'addfav.php',
          type: 'POST',
          data: {'id':jQuery(btn).closest("div").attr("id"),is_ajax: 1},
          success: function(html) {
            jQuery('#button').removeClass('before');
            jQuery('#button').addClass('after');
            jQuery('#button').attr('disabled', 'disabled');
          }, 
          error: function() {
            jQuery('#error').html('<div>Error! Unable to add food item.</div>');
          }
        });
    }
    

    Within the inline attribute this does refer to the clicked element, but not within the function you’re calling (unless you take steps to make it so with .call() or .apply()).

    However, I’d recommend binding the event handler with jQuery:

    jQuery("input.before").click(function(){
       jQuery.ajax({       
          url: 'addfav.php',
          type: 'POST',
          data: {'id':jQuery(this).closest("div").attr("id"),is_ajax: 1},
          success: function(html) {
            jQuery('#button').removeClass('before');
            jQuery('#button').addClass('after');
            jQuery('#button').attr('disabled', 'disabled');
          }, 
          error: function() {
            jQuery('#error').html('<div>Error! Unable to add food item.</div>');
          }
        });
    });
    

    …and then jQuery makes sure this is set appropriately, and also you don’t need the inline onclick. Put the above in a document.ready handler or in a script block that appears after the button (e.g., at the end of the body).

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

Sidebar

Related Questions

I'm trying to get the IDs from my parent divs <div id=btn1><label class=btn><input type=submit
i'm trying to submit my Ajax form using jQuery. However calling the submit() function
I'm trying to submit a simple jQuery ajax call but using the DELETE method
I got a <a> that contains a <input type=text> clicking the <a> will submit
I'm trying to submit a form when a select changes using jQuery Mobile. I
I'm trying to submit a few forms through a Python script, I'm using the
Trying a bit of AJAX, and I find that much of my data is
I'm trying to animate the height of the closest parent of a toggle button:
So, I'm having difficulty trying to append/add a child div to the parent div,
This is what I am trying to do: Once submit a form on my

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.