Skip to main content

jquery to serialize only elements within a div



I would like to get the same affect as jQuery.serialize() but I would like to return only the child elments of a given div.





sample result:







single=Single2&multiple=Multiple&radio=radio1





Source: Tips4all

Comments

  1. No problem. Just use the following. This will behave exactly like serializing a form but using a div's content instead.

    $('#divId *').serialize();


    Check http://jsbin.com/azodo for a demonstration (http://jsbin.com/azodo/edit for the code)

    ReplyDelete
  2. You can improve the speed of your code if you restrict the items jQuery will look at.

    Use the selector :input instead of * to achieve it.

    $('#divId :input').serialize()


    This will make your code faster because the list of items is shorter.

    ReplyDelete
  3. Try also this:


    $('#divId').find('input').serialize()

    ReplyDelete
  4. jitter's solution helped me from a more generic viewpoint. Clicking a link, finding it's parent div and then serializing:

    //$(this) is a "click"able item within the div
    var divid = $(this).closest('div').attr('id);
    var data = $('#'+ divid+ ' *').serialize();

    ReplyDelete

Post a Comment

Popular posts from this blog

Slow Android emulator

I have a 2.67 GHz Celeron processor, 1.21 GB of RAM on a x86 Windows XP Professional machine. My understanding is that the Android emulator should start fairly quickly on such a machine, but for me it does not. I have followed all instructions in setting up the IDE, SDKs, JDKs and such and have had some success in staring the emulator quickly but is very particulary. How can I, if possible, fix this problem?