Is it possible to get the serialized list of items from a UL in jquery by calling the serialize method directly instead of using a callback? The code snippet:
var sortableLinks = $("#category_links_list_3");
var linkOrderData = $(sortableLinks).sortable('serialize');
category_links_list_3 is the id of the UL
The DOM structure is:
<div class="hidden" id="inline_list_3">
<ul class="category_links_list ui-sortable" id="category_links_list_3">
<li class="link_title ui-state-default" id="category_link_8">Coconut Oil</li>
<li class="link_title ui-state-default" id="category_link_9">Hempseed</li>
</ul>
</div>
Thanks...
I finally got the answer! You need to make the UL sortable first before calling the serialize method on it:
This time linkOrderData contains category_link[]=8&category_link[]=9
I was able to get this function working using the split. If you have multiple underscores in your class you may need to adjust the index
If serialize returns an empty string, make sure the id attributes include an underscore. They must be in the form: "set_number" For example, a 3 element list with id attributes
foo_1
,foo_5
,foo_2
will serialize tofoo[]=1&foo[]=5&foo[]=2
. You can use an underscore, equal sign or hyphen to separate the set and number. For examplefoo=1
orfoo-1
orfoo_1
all serialize tofoo[]=1
.Above one is a example. that i used it. That is why I saw 2 you.
http://jqueryui.com/demos/sortable/#method-serialize
it migth be help you.
HTML
JQuery
Result
Hope this help!
var formStr = $('#container').serialize()
Added: That will work for form elements. You could also roll your own serialize like so:
This post was very helpful. In case you're looking for additional help, here's how I got it to work (using haml-rails). Using the toArray function is slightly different. If using `serialize' you would have to set the attribute, again, as 'data-item="data-item_#{id}'.
Thank you, Internet, for knowing so many programming solutions.