how do i pull individual information from an array?

You gotta add value to the <option> and pass it’s value to the showEmployee function. Check out the working snippet

<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Lab 9-1: Employee Database</title>

    <script language="JavaScript" type="text/javascript">


        // Complete the employeeObject constructor
        // Remember t o add a method called showEmployee
        function employeeObject(name, department, extension) {
            []
            this.name = name;
            this.department = department;
            this.extension = extension;
            this.showEmployee = showEmployee;
        }

        // Instantiate 3 instances of employeeObject
        // Important - Start your array index numbers with 1
        var employees = new Array();
        employees[0] = "Select Employee";
        employees[1] = new employeeObject("Mai Li", "Sales", 551);
        employees[2] = new employeeObject("Maria Alvarez", "Human Resources", 441);
        employees[3] = new employeeObject("Tom Smith", "Marketing", 331);

        len = employees.length;

        function showEmployee(i) {
            var info = "";
            info += "Employee: " + employees[i].name + "\n";
            info += "Department: " + employees[i].department + "\n";
            info += "Extension: " + employees[i].extension + "\n\n";
            alert(info);
        }

        function showAllEmployees() {
            var info = "";
            for (var i = 1; i < len; i++) {
                info += "Employee: " + employees[i].name + "\n";
                info += "Department: " + employees[i].department + "\n";
                info += "Extension: " + employees[i].extension + "\n\n";
            }

            alert(info);
        }

    </script>
</head>

<body>
    <h3>Employee Database</h3>
    <hr />
    <form name="empForm" id="empForm">
        <strong>Select name to view information:</strong>
        <select name="empName" onchange="showEmployee(this.value);">
            <script language="JavaScript" type="text/javascript">
                for (var i = 0; i < len; i++) {
                    if (i == 0) document.write('<option value="">' + employees[i]) + "</option>";
                    else document.write('<option value="'+i+'">' + employees[i].name) + "</option>";
                }
            </script>
        </select>
        <p>
            <input type="button" value="Show All Employees" onclick="showAllEmployees();" />
        </p>
    </form>
</body>

</html>

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top