0

enter image description here

I am trying to keep track of changes to a select box in django. My code below is working up to alert(change.new_time);, so I am making the object correctly-

        var changed_select_box_array = [];


        function handleChanges(id){
            var x = document.getElementById(id).selectedIndex;
            var time = document.getElementsByTagName("option")[x].value;
            var change = {id:id, new_time:time};
            alert(change.id);
            alert(change.new_time);
            changed_select_box_array[changed_select_box_array.length] = change;
            alert(changed_select_box_array[0].id);
        }

but I cannot access the new item in the array. I tried 4-5 different ways and followed some rules for global variables in funcs I found on this site, and I cannot access anything from the new array. Am I doing something wrong adding to the array? I tried push too. Thank you

4
  • Why are you writing to changed_select_box_array[changed_select_box_array.length], but reading the entirely different element changed_select_box_array[0]? Commented May 28, 2015 at 18:49
  • i tried to write using push first but still couldn't access it. seems to me my issue is the accessing of it Commented May 28, 2015 at 18:50
  • it's working for me. What is your result for 2 first alert? Commented May 28, 2015 at 18:50
  • changing the 3rd box to 12:00 am yields id_open_time_2 and 00:00 (the hidden value ) Commented May 28, 2015 at 18:53

1 Answer 1

1

You can use Object as associative array.

var changed_select_box_array = {};

function handleChanges() {
    var x = this.selectedIndex;
    var id = this.id;
    var time = this.getElementsByTagName("option")[x].value;
    var change = { id: id, new_time: time };
    changed_select_box_array[id] = change;
    console.log(changed_select_box_array);
}
<!--Emitation of some select inputs with change events-->
<select id="s1" onchange="handleChanges.call(this)">
    <option value="val1">Value 1</option>
    <option value="val2">Value 2</option>
    <option value="val3">Value 3</option>
</select>

<select id="s2" onchange="handleChanges.call(this)">
    <option value="val4">Value 1</option>
    <option value="val5">Value 2</option>
    <option value="val6">Value 3</option>
</select>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.