0

I am trying to call a function I defined within an object block, but only whenever a select field value is changed.

var student = {
  role: function() {
    var studRole = document.getElementById("student");
    var roleStud = studRole.options[studRole.selectedIndex].value;
    switch (roleStud) {
      case 'Admin':
        alert("Welcome Admin");
        break;
      default:
    }
  }
}
<form>
  <select id="countries" onchange="test()">
    <option value="France">France</option>
    <option value="Nigeria">Nigeria</option>
  </select>
  <br/>Student Role:
  <select id="student" onchange="student.role()">
    <option value="user">User</option>
    <option value="Admin">Admin</option>
  </select>
</form>

Fiddle Demo

4
  • On which line does this error occur? When you trace through your code in the debugger, what is the value of studRole? Commented Nov 16, 2016 at 6:25
  • Would most probably be a problem with scope of student object, where did you define that? Commented Nov 16, 2016 at 6:26
  • 5
    You have conflicting ids with the HTML element named student and the object named student. Remember HTML IDs are put on the global object. Commented Nov 16, 2016 at 6:29
  • @torazaburo — Too good! Commented Nov 16, 2016 at 6:30

3 Answers 3

1

Considering student is in the window scope, you can refer to it as in window.student.role()

var student = {
            role: function () {

                var studRole = document.getElementById("student");
                var roleStud = studRole.options[studRole.selectedIndex].value;

                switch (roleStud) {
                    case 'Admin':
                        alert("Welcome Admin");
                        break;
                    default:
                    }
                  }
                }
<form>
        <select id="countries" onchange="test()">
            <option value="France">France</option>
            <option value="Nigeria" >Nigeria</option>
        </select><br/>
        Student Role:
        <select id="student" onchange="window.student.role()">
            <option value="user">User</option>
            <option value="Admin" >Admin</option>
        </select>
</form>

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

1 Comment

Why would you need to do that?
0

You need to call function with object you are calling it with a string

 <form>
        <select id="countries" onchange="test()">
            <option value="France">France</option>
            <option value="Nigeria" >Nigeria</option>
        </select><br/>
        Student Role:
        <select id="student" onchange="CheckRole()">
            <option value="user">User</option>
            <option value="Admin" >Admin</option>
        </select>
</form>
    <script>
var student = {
            role: function () {

                var studRole = document.getElementById("student");
                var roleStud = studRole.options[studRole.selectedIndex].value;

                switch (roleStud) {
                    case 'Admin':
                        alert("Welcome Admin");
                        break;
                    default:
                    }
                  }
}
function CheckRole() {
    student.role();
}

Comments

0

Please define your function first.You are calling a "test" function which is not defined in your script.

<!DOCTYPE html>
 <html>
 <head>
<style>
table, td, th {
border: 1px solid black;
 }

table {
  border-collapse: collapse;
  width: 100%;
}

th {
height: 50px;
}
</style>
</head>
 <body>

 <form>
    <select id="countries" onchange="test()">
        <option value="France">France</option>
        <option value="Nigeria" >Nigeria</option>
    </select><br/>
    Student Role:
    <select id="student" onchange="window.student.role()">
        <option value="user">User</option>
        <option value="Admin" >Admin</option>
    </select>
    </form>
    <script>

 function test(){
 var student = {
          role: function () {

            var studRole = document.getElementById("student");
            var roleStud = studRole.options[studRole.selectedIndex].value;

            switch (roleStud) {
                case 'Admin':
                    alert("Welcome Admin");
                    break;
                default:
                }
              }
            }
}
</script>
</body>
</html>

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.