11

Trying to serialize just the elements from a specific table but it only returns a result if i do the whole Form

in the below code, i want to ajax just the elements in tbl2

<form>
 <input type="text" id="tb1" name="tbl1"/>
  <table name="tbl1">
   <tr><td><input type="text" name="tb2"/></td></tr>
 </table>
 <table name="tbl2">
   <tr><td><input type="text" name="tb3"/></td></tr>
   <tr><td><input type="text" name="tb4"/></td></tr>
 </table>
</form>

the code

var params = $("#tbl2").serialize();

var resp = $.ajax({
    async: false,
    type: "POST",
    url: AppRoot + "webhandlers/postback.ashx",
    data: params
});

3 Answers 3

23

First and foremost, a <table> cannot have a name attribute, and even if it could, the jQuery ID selector (#) would not match it.

If you use id instead (<table id="tbl2">), it will work like this:

var params = $("#tbl2 :input").serialize();

The :input selector selects all the form elements (here, inside #tbl2), it is needed because serialize() will only work on those.

Please also check out my jsFiddle Demo.

4
  • @Deeptechtons You're free to try it in the Fiddle I have posted, but I assure you it won't work.
    – kapa
    Sep 28, 2011 at 11:04
  • 1
    i actually have id in my code, i must have mistakenly put in name when i wrote the code in this question. sorry.
    – Christian
    Sep 28, 2011 at 11:06
  • @Christian Then you don't have to change the name, just use serialize() the way I have shown.
    – kapa
    Sep 28, 2011 at 11:08
  • 1
    @bazmegakapa also this post might help the op with getting the inputs only with value stackoverflow.com/questions/608730/… Sep 28, 2011 at 11:09
2

you cannot serialize a table -- that method doesn't apply to that kind of DOM object, only forms and form fields can be serialized.

if you really want to do what you're proposing, you need the proper selector to pick just the children of tbl2 that are also form elements, and then you'll have to serialize each one of those by hand. someone did it in another question, here: serialize without a form?

a better way might be to disable all the form elements that are NOT in the table you're interested in -- you'll need a selector to pick all form elements that are not child elements of tbl2 -- and THEN serialize the form. the disabled elements will be omitted.

1
  • 1
    I don't think disabling form elements would be any close to a better way.
    – kapa
    Sep 28, 2011 at 11:13
1

you can use the serializeArray method, which will give you the array of input fields and can be used with the data.

var params = $("#tbl2 input").serializeArray();
2
  • Please note that your selector will only match <input> elements, and won't match other possible form elements like <select> or <textarea>.
    – kapa
    Sep 28, 2011 at 11:10
  • yup its only for input as in example html, if you need additional form elements they need to be included.
    – Jayendra
    Sep 28, 2011 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.