TheWandererr TheWandererr - 1 year ago 96
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")
.setAttribute("V", "V_1")
. I already can get an array of an singel Attribute using the

$("#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>

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.

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] ];

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

Answer Source

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];

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download