Please have a look on the code below ..
<div id="click_me">Save</div>
<div id="blocks_sortable">
<div id="block_1">
<h2>Block 1</h2>
<div class="items_sortable connectedSortable">
<div id="item_1">
<span>Item 1</span></div>
<div id="item_2">
<span>Item 2</span></div>
<div id="item_3">
<span>Item 3</span></div>
</div>
</div>
<div id="block_2">
<h2>Block 2</h2>
<div class="items_sortable connectedSortable">
<div id="item_4">
<span>Item 4</span></div>
<div id="item_5">
<span>Item 5</span></div>
<div id="item_6">
<span>Item 6</span></div>
</div>
</div>
</div>
<script>
$("#click_me").click(function() {
var result = $("#blocks_sortable > div");
for(var i=0; i<result.length; i++){
var str = "";
var str2 = "";
var block = result[i]; //div object
//retrieve block id and create the string with id
str += "block="+$(block).attr("id")+"&items=";
//trying to select all the items of the current Block
var result2 = $(block+" > div");
for(var j=0; j<result2.length; j++){
var item = result2[j];
str2 += $(item).attr("id")+",";
} //end for items
str = str+str2;
// looking for a final string in the format of .. block=block_1&items=item_1,item_2,item_3 for loop 1
alert(str);
}
});
</script>
It is not working. Is there any easier and working solution to get these ids. Please help.
Thank you.
I think you are getting an error when you call
str += "block="+$(block).attr("id")+"&items=";
.attr is not working because block is a dom element and not a jquery object. You are having the same issues with var item too.
The working solution is here. And the demo is here.
This javascript fixes it:
try without ">"
jQuery map function is your friend
Will return
"block_1=item_1,item_2,item_3,block_2=item_4,item_5,item_6"
, which gets you much closer to where you're going (I think).EDIT: Solution and source here.