views:

100

answers:

4

Hello,

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.

+2  A: 

jQuery map function is your friend

$.map( 
    $("#blocks_sortable > div"),
    function(elementOfArray){
        var block = $(elementOfArray);
        var children = $.map(
            $("div[id]", block),
            function( eoa ){
                return eoa.id;
            }).join(",");

        return elementOfArray.id + "=" + children;
    }
).join(",");

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.

R0MANARMY
Great .. I like your solution. Thanks.
A: 

try without ">"

var result = $("#blocks_sortable div");
Sam3k
That will also select the **<div class="items_sortable connectedSortable">** div giving him an element without an ID.
R0MANARMY
+1  A: 

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:

   $("#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.id +"&items=";

                //trying to select all the items of the current Block
                var result2 = $("#" + block.id + "> div > div");
                    for(var j=0; j<result2.length; j++){
                        var item = result2[j];

                        str2 += item.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);
            }
    });
Jon
Adding links for working solution, very nice. I will blatantly copy you sir!
R0MANARMY
@Jon +1 .. you r great .. it works .. but I have accepted @ROMANARMY solution as its easier me to achieve what i want to do that way.
@R0MANARMY, Thanks for adding your solution as well, and it would be great if you could put a demo up. I've learned something new for sure! @user187580, I'm glad you found your solution! That's what we're here for, I'm going to give R0MANARMY +1 on his solution too, I agree it's better, and very cool :)
Jon
A: 
    $("#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].id; //div object

            //retrieve block id and create the string with id
            //str += "block="+$(block).attr("id")+"&items=";
            str += "block="+ block + "&";
            //trying to select all the items of the current Block
            var inner_span = "#" + block + " > div";
            var result2 = $(inner_span).children();

                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); // output block=block_1&item_1,item_2,item_3 
                        //        block=block_2&item_4,item_5,item_6

        }
SteD