Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get attritubes of form input when generating a form dynamically and using .live()
    primarykey
    data
    text
    <p>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: <a href="https://stackoverflow.com/questions/2754961/rebind-dymanically-created-forms-after-jquery-ajax-response">Rebind dymanically created forms after jQuery ajax response</a>**</p> <p>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.</p> <p>Here's the code:</p> <pre><code>&lt;script type="text/javascript"&gt; $().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&lt;data.length) { $('div#messages').append( '&lt;div id="msg-'+data[i].id+'" style="border: 1px coral solid; margin: 5px 5px 5px 5px;"&gt;'+ '&lt;form id="message-'+data[i].id+'" name="form-'+data[i].id+'" action="/path/to/ajaxhandler.php" &gt;'+ 'From: '+data[i].u_from+' ----&gt; To: '+data[i].u_to+'&lt;br /&gt;'+ 'Subject: &lt;br /&gt;&lt;input name="subject" type="text" value="'+data[i].subject+'" /&gt; &lt;br /&gt;&lt;br /&gt;'+ 'Message: &lt;br /&gt;&lt;textarea rows="10" cols="60"&gt;'+data[i].text+'&lt;/textarea&gt;&lt;br /&gt;'+ '&lt;input class="formApprove" type="submit" id="approve-'+data[i].id+'" value="Approve" /&gt; or '+ '&lt;input class="formDeny" type="submit" id="deny-'+data[i].id+'" value="Deny" /&gt;'+ '&lt;/form&gt;&lt;/div&gt;'); i++; } }); } } &lt;/script&gt; </code></pre> <p>Here is what I need to change to be correct:</p> <pre><code>// this is not the value of the class attribute from submit button clicked. var action = this.attr('class'); alert(action); </code></pre> <p>So I can get the class attribute from the button that was clicked.</p> <p>UPDATE (8.27.2010) - Here's what I did for anyone who stumbles across this and needs an answer:</p> <p>For the serializeObject() function, see this post: <a href="https://stackoverflow.com/questions/1184624/serialize-form-to-json-with-jquery">Convert form data to JavaScript object with jQuery</a></p> <pre><code>$(".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; }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload