TheWandererr TheWandererr - 2 months ago 7
HTML Question

Get all Element Attributes from a Sortable list

So i have a List of divs which are sortable using the Jquery UI option sortable.
Those divs contain some custom attributes using the setAttribute method like:

.setAttribute("Nr", "Nr_1")
and
.setAttribute("V", "V_1")
. I already can get an array of an singel Attribute using the
.toArray
method


$("#NameContainer").sortable("toArray", { attribute: 'value'});



$("#NameContainer").sortable("toArray", { attribute: 'Nr' });



$("#NameContainer").sortable("toArray", { attribute: 'V' });





<div id="NameContainer" class="ui-widget">
<div value="Name_1" id="Name_1">John</div>
<div value="Name_2" id="Name_2">Jack</div>
<div value="Name_3" id="Name_3">Charlie</div>
<div value="Name_4" id="Name_4">Sawyer</div>
<div value="Name_5" id="Name_5">Yin</div>
<div value="Name_6" id="Name_6">Ben</div>
</div>


My Question now is how can i get all Attributes from every Elemente in the right order after they got sorted or course in a singel variable.


UPDATE:
So my first solution that is working as intented looks like that:

for(var i = 0; i<ArrayList.length; i++){
var attributeArray = [arrayValue[i], arrayNr[i], arrayV[i] ];
mainArray.push(attributeArray);
}


And then call the main:
mainArray[ ] [1]

Answer

EDIT: Was only doing it for the main div container..fixed:

If you want to get the attributes in the order they are found then I think you can do something along the lines of:

var el = $('#NameContainer > div');

var nodes=[], values=[];
el.each(function() {
    for (var att, i = 0, atts = $(this).attributes, n = atts.length; i < n; i++){
        att = atts[i];
        nodes.push(att.nodeName);
        values.push(att.nodeValue);
    }
});

that will give you two arrays where the index i of nodes will give you the attribute name and the same index i of values will be the accompanying value of that attribute. this can be restructured in a number of different ways but this is a simple way to achieve your goal.

Comments