Issue on Ordering - Indexing Multidimensional Array
Can you please take a look at
This demo let me know how I can force Javascript to push the items into the array based on the items id?
<!-- begin snippet: js hide: false -->
<!-- language: lang-js -->
- var theArray = [
- ["A", "B", "C"],
- ["D", "E"],
- ["F", "G", "H", "I"],
- ["J"],
- ["K", "L", "M"]
- ];
- var res = theArray.toString();
-
- $('#textarea').text(res);
-
- $("input[name='m1']").change(function () {
- if ($(this).is(':checked')) {
- var id=$(this).attr('id');
- var selected = $(this).val();
- theArray[1][id] = selected;
-
- } else {
- var itemtoRemove = $(this).val();
- theArray[1].splice($.inArray(itemtoRemove, theArray[1]),1);
-
- }
- var res = theArray.toString();
- $('#textarea').text(res);
-
- });
-
-
- <!-- language: lang-html -->
-
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
- <br />
- <textarea id="textarea" rows="2" cols="100"></textarea>
- <br />
- <br />
- <fieldset>
- <legend>Items</legend>
- <input type="checkbox" id="1" name="m1" value="mItem1" />mItem 1
- <br />
- <input type="checkbox" id="2" name="m1" value="mItem2" />mItem 2
- <br />
- <input type="checkbox" id="3" name="m1" value="mItem3" />mItem 3
- <br />
- <br />
- </fieldset>
As you can see the example works if you un check from `mItem 3` to `mItem 3` but if you uncheck `mItem 1` first and check it again it will overwrite the mitem2 in the array (which make seance!)
Can you please let me know how i can fix this?
Thanks