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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:45:33+00:00 2026-05-16T11:45:33+00:00

I am dynamically creating forms from a JSON object that I fetch with a

  • 0

I am dynamically creating forms from a JSON object that I fetch with a getJSON. If there is a better way of building forms in this manner, please let me know. I used this example to understand how to rebind submit button:
Rebind dymanically created forms after jQuery ajax response**

What I need to know is how to grab the class of the input submit button I have clicked. There’s an “Approve” and “Deny” button and depending on which one is clicked will be sent to the server in a POST so I know what to do with the form data being POST’ed on the server side.

Here’s the code:

<script type="text/javascript">
$().ready(function(){
    jQuery("form[id^='message-']").live('submit', function(){

        var id = parseInt(this.id.replace("message-", ""));

        // this is not the value of the class attribute from submit button clicked.      
        var action = this.attr('class');
        alert(action);

        return false;

        jQuery.ajax({
            type: "POST",
            url: "/path/to/ajaxhandler.php",
            data: {"action": action, "id": id},
            success: function(response) {                
        }           
    });

    /*
        generate form
    */
    $.fn.getAllUnapprovedMessages = function () {
        $.getJSON("/messages.php?a=getAllUnapproved, function(data){
            var i=0;
            $("div#messages").empty();

            while(i<data.length) {
                $('div#messages').append(
                    '<div id="msg-'+data[i].id+'" style="border: 1px coral solid; margin: 5px 5px 5px 5px;">'+
                    '<form id="message-'+data[i].id+'" name="form-'+data[i].id+'" action="/path/to/ajaxhandler.php" >'+
                    'From: '+data[i].u_from+' ----> To: '+data[i].u_to+'<br />'+
                    'Subject: <br /><input name="subject" type="text" value="'+data[i].subject+'" /> <br /><br />'+
                    'Message: <br /><textarea rows="10" cols="60">'+data[i].text+'</textarea><br />'+
                    '<input class="formApprove" type="submit" id="approve-'+data[i].id+'" value="Approve" /> or '+
                    '<input class="formDeny" type="submit" id="deny-'+data[i].id+'" value="Deny" />'+
                    '</form></div>');
                i++;
            }
        });
    }
}
</script>

Here is what I need to change to be correct:

// this is not the value of the class attribute from submit button clicked.      
var action = this.attr('class');
alert(action);

So I can get the class attribute from the button that was clicked.

UPDATE (8.27.2010) – Here’s what I did for anyone who stumbles across this and needs an answer:

For the serializeObject() function, see this post: Convert form data to JavaScript object with jQuery

$(".formApprove").live('click', function() {
    var fid = this.id.replace(/approve-/i, "");
    var formObject = $('form#message-'+fid).serializeObject();

    $.post('/path/to/ajaxhandler.php', {
        a: 'setApproved',
        id: fid,
        subject: formObject.subject,
        message: formObject.message
    }, function(res) {
        if(res.result == 0) {
            alert ('done');
        } else {
            alert ('Error');
    },"json");
    return false;
});
  • 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-05-16T11:45:33+00:00Added an answer on May 16, 2026 at 11:45 am

    Short answer is, you can’t. You can’t get a reference to the element that caused the form to submit from the form’s submit event alone. You’ll need to wire up handlers to the buttons’ click events too. If you give the input tags a name attribute, their value will be sent to the server with the rest of the form values during a normal, full post, but that doesn’t help much when you want to do it via Ajax.

    I’d suggest you modify the code to handle the buttons’ click events and in that handler harvest the class/value attribute, then trigger the form’s submit event and pass the attribute value in as custom event data, which you can then read from the form submit handler. If the passed data is null/undefined.

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

Sidebar

Ask A Question

Stats

  • Questions 510k
  • Answers 510k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer From what I understand, you always want your initial conditions… May 16, 2026 at 5:08 pm
  • Editorial Team
    Editorial Team added an answer By "evaluate", do you mean some kind of unification or… May 16, 2026 at 5:08 pm
  • Editorial Team
    Editorial Team added an answer I think a ListBox could work. With a little bit… May 16, 2026 at 5:08 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am creating a form dynamically from the fields returned from server using json
I am creating a JSON object dynamically and when I send it via an
I am creating a form that uses MultipleChoiceField. The values for this field are
I'm working on an application that gets content from feeds in C#. This content
I am creating a table dynamically based on the number of rows returned from
Possible Duplicate: Best way to stop SQL Injection in PHP I am creating a
I have a WPF ListView control for which I am dynamically creating columns. One
I am creating NSButton dynamically in the cocoa application which is fine. However once
I am creating a table (stats_1) dynamically and would like to have each row
I am creating input boxes dynamically to capture inputs for a certain word (e.g.

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.