0

I am trying to call an array in javascript from the input the user selects on the webpage..

<select id="weekdayOptions">
            <option>Monday</option>
            <option>Tuesday</option>
            <option>Wednesday</option>
            <option>Thursday</option>
            <option>Friday</option>
        </select>

javascript is ...

var selectDay = document.getElementById('weekdayOptions').value;

var Monday = ["hi" , "hello" , "test"];

var Tuesday = ["hi" , "hello" , "test"];

var Wednesday = ["hi" , "hello" , "test"];

var Thursday = ["hi" , "hello" , "test"];

var Friday = ["hi" , "hello" , "test"];

alert(selectDay[2]);

the values of the array are only the same for an example, in my code they are different, but what i am trying to do is call the array for the weekday chosen by the input of the user, hope i explained this clearly, any help is great, cheers.

3 Answers 3

3

You are trying to use a string (e.g. "Monday") to reference a variable name, and that won't work (without eval, which you should avoid). You'd be better off having the days of the week be object keys.

var options = document.getElementById('weekdayOptions');
var weekdays = {
  Monday: ["hi" , "hello" , "test"],
  Tuesday: ["hi" , "hello" , "test"],
  Wednesday: ["hi" , "hello" , "test"],
  Thursday: ["hi" , "hello" , "test"],
  Friday: ["hi" , "hello" , "test"]
};

options.onchange = function() {
  var selectDay = this.value;
  alert(selectDay + ': ' + weekdays[selectDay][2]);
}
<select id="weekdayOptions">
  <option>Monday</option>
  <option>Tuesday</option>
  <option>Wednesday</option>
  <option>Thursday</option>
  <option>Friday</option>
</select>

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

Comments

0

This is the approach I would recommend:

HTML

<select id="weekdayOptions">
    <option value="M">Monday</option>
    <option value="T">Tuesday</option>
    <option value="W">Wednesday</option>
    <option value="Th">Thursday</option>
    <option value="F">Friday</option>
</select>

JavaScript

var days = {
    M: ['monday 1', 'monday 2', 'monday 3'],
    T: ['tuesday 1', 'tuesday 2', 'tuesday 3'],
    W: ['wednesday 1', 'wednesday 2', 'wednesday 3'],
    Th: ['thursday 1', 'thursday 2', 'thursday 3'],
    F: ['friday 1', 'friday 2', 'friday 3']
}

var daySelect = document.getElementById('weekdayOptions');
daySelect.onchange = function() {
    alert(days[this.value][2]);
};

Try it out here.

Comments

0

Another approach would be the following. Take care so that you assign a default value in case the user leaves it on Monday without touching the selectbox.

JSFIDDLE here

HTML

<select id="weekdayOptions">
    <option>Monday</option>
    <option>Tuesday</option>
    <option>Wednesday</option>
    <option>Thursday</option>
    <option>Friday</option>
</select>

Javascript

var selectDay = document.getElementById('weekdayOptions'); 
var selOptionArray = ["MONhi" , "hello" , "MONtest"]; // Default for Monday: If the selectbox is left unchanged there is no value assigned to the array.

selectDay.onchange = function(){

    switch(selectDay.value){
        case 'Monday':
            selOptionArray = ["MONhi" , "hello" , "MONtest"];
            break;
        case 'Tuesday':
            selOptionArray = ["TUEhi" , "hello" , "TUEtest"];
            break;
        case 'Wednesday':
            selOptionArray = ["WEDhi" , "hello" , "WEDtest"];
            break;
        case 'Thursday':
            selOptionArray = ["THUhi" , "hello" , "THUtest"];
            break;
        case 'Friday':
            selOptionArray = ["FRIhi" , "hello" , "FRItest"];
            break;
        default:
            selOptionArray = "";
            break;
    }

     alert(selOptionArray[2]);
}

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.