How to sort div container using javascript

Bharat Guru
Bharat Guru used Ask the Experts™
on
using jquery how can I dynamically update the data-index value and rearrange the div container.  

<div>
    <div id="divA" data-index="1"></div>
    <div id="divB" data-index="3"></div>
    <div id="divC" data-index="2"></div>
</div>

Open in new window


loking for function as
function sort(a){
     if a = 'ByProd' {
                divA.data-index=1;
                divB.data-index=3;
                divC.data-index=2;
   }else if a = 'ByProd' {
                divA.data-index=3;
                divB.data-index=1;
                divC.data-index=2;
 }else{
                divA.data-index=1;
                divB.data-index=2;
                divC.data-index=3;
}

}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
leakim971Multitechnician
Top Expert 2014

Commented:
test page : https://jsfiddle.net/7910asdo/
function sort(a){
	switch(a) {
     	case "ByProd": 
      	$("#divA").data("index", 1);
      	$("#divB").data("index", 3);
      	$("#divC").data("index", 2);
        break;
			case "BySome":
      	$("#divA").data("index", 3);
      	$("#divB").data("index", 1);
      	$("#divC").data("index", 2);
        break;
			default:
      	$("#divA").data("index", 1);
      	$("#divB").data("index", 2);
      	$("#divC").data("index", 3);
	}
}

Open in new window

Author

Commented:
how to do same using javascript
leakim971Multitechnician
Top Expert 2014

Commented:
why did you add jQuery topics on your question if you want only pure Javascript answer?
Multitechnician
Top Expert 2014
Commented:
        function sort(a){
            var divA = document.getElementById("divA");
            var divB = document.getElementById("divB");
            var divC = document.getElementById("divC");
            switch(a) {
                case "ByProd":
                    divA.dataset.index =  1;
                    divB.dataset.index =  3;
                    divC.dataset.index =  2;
                    break;
                case "BySome":
                    divA.dataset.index =  3;
                    divB.dataset.index =  1;
                    divC.dataset.index =  2;
                    break;
                default:
                    divA.dataset.index =  1;
                    divB.dataset.index =  2;
                    divC.dataset.index =  3;
            }
        }

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial