views:

279

answers:

3

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...

A: 

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 to foo[]=1&foo[]=5&foo[]=2. You can use an underscore, equal sign or hyphen to separate the set and number. For example foo=1 or foo-1 or foo_1 all serialize to foo[]=1.

above one is a example. that i used it. thats why. i saw 2 you.

http://jqueryui.com/demos/sortable/#method-serialize

it migth be help you.

Sikender
I have the properly formatted ids in place. I could use the callback method, but as you can see from the DOM structure, the UL is not visible and therefore cannot fire events.
kalengi
+1  A: 

var formStr = $('#container').serialize()

Added: That will work for form elements. You could also roll your own serialize like so:

function serializeList(container)
{
  var str = ''
  var n = 0
  var els = container.find('li')
  for (var i = 0; i < els.length; ++i) {
    var el = els[i]
    var p = el.id.lastIndexOf('_')
    if (p != -1) {
      if (str != '') str = str + '&'
      str = str + el.id.substring(0, p) + '[]=' + (n + 1)
      ++n
    }
  }
  return str
}

alert(serializeList($('#container')))
jspcal
I tried this and it doesn't work. Thanks!
kalengi
if no form ids, you can roll your own function as above
jspcal
This definitely replicates the serialize functionality. I however located the reason why the method wasn't returning anything as I've detailed in the answer below.
kalengi
+1  A: 

I finally got the answer! You need to make the UL sortable first before calling the serialize method on it:

var sortableLinks = $("#category_links_list_3");
$(sortableLinks).sortable();
var linkOrderData = $(sortableLinks).sortable('serialize');

This time linkOrderData contains category_link[]=8&category_link[]=9

kalengi