0

So when my value is gsm i just need an alert with ("nice it works") but it doesn't want to work! Can you guys help me out? Without changing to much of my code? Unless it's really wrong ofcourse.

This is my Html:

<select id="telefoonkeuze" onchange="Checkfunction()">
        <option value="gsm" id="gsm">gsm</option>
        <option value="telefoon">telefoon</option>
</select>

This is my js:

        var gsm = document.getElementById("gsm");

        function Checkfunction() {
            var x = document.getElementById("telefoonkeuze").value;
            if (x == gsm) {
                alert("nice it works!");
            }
        }
haim770
  • 48,394
  • 7
  • 105
  • 133
Wesley
  • 83
  • 2
  • 9
  • 3
    `value` is a `string` containing the chosen option value, not the actual ` – haim770 Mar 25 '15 at 16:26
  • Make sure that the dom has been builtwhen you assign your `gsm` variable ( place it in an `onload` handler or use jquery' s `ready` handler). – collapsar Mar 25 '15 at 16:34

4 Answers4

3

when you do this

var gsm = document.getElementById("gsm");

you are assigning an element to your gsm variable. Then later comparing that element to a value.

just change that line

var gsm='gsm';

or elimate the variable and put the 'gsm' string in your if statement.

also you may have trouble getting the selected value as you are. there is another thread about that though. Get selected value in dropdown list using JavaScript?

Community
  • 1
  • 1
RightClick
  • 1,090
  • 7
  • 12
0

var Checkfunction = function() {
  var x = document.getElementById("telefoonkeuze");
  if (x.value == "gsm") {
    alert("here you go");
  }
}
<select id="telefoonkeuze" onchange="Checkfunction()">
  <option value="gsm" id="gsm">gsm</option>
  <option value="telefoon">telefoon</option>
</select>
Puffy
  • 30
  • 6
  • Ok this works! But can you tell me what's wrong with this if? if (x.value == "gsm") { alert("here you go") } else if (x.value == "huistelefoon") { alert("nice it works") } else ("somethings wrong!"); } – Wesley Mar 25 '15 at 17:11
  • @Wesley: 1.) your last 'else' lacks an opening brace and a call of 'alert'/'console.log'. 2.) there is no element with value 'huistelefoon'. – collapsar Mar 25 '15 at 18:43
0

You can do it this. http://jsfiddle.net/c8go6663/2/

 var gsm = document.getElementById("gsm").value;

        Checkfunction = function() {
            var x = document.getElementById("telefoonkeuze").value;
            if (x == gsm) {
                alert("nice it works!");
            }
        }
Maybe Gordon
  • 130
  • 5
0

The following section is a pure JS modification of your code. You may view it in this fiddle too (adapted to the jsfiddle environment):

<html>
  <head>
    <title>getElementById fails</title>
    <script type="text/javascript">
        var gsm;

        function Checkfunction() {
            var x = document.getElementById("telefoonkeuze").value;
            if (x === gsm) {
                alert("nice it works!");
            }
        }

        window.addEventListener ( "load", function () { gsm = document.getElementById('gsm').value; 1; });
    </script>
  </head>
  <body>
      <div>
          <select id="telefoonkeuze" onchange="Checkfunction()">
                  <option value="gsm" id="gsm">gsm</option>
                  <option value="telefoon">telefoon</option>
          </select>
     </div>
  </body>
</html>

Note

This answer is supposed to be robust and working. Credits (and acceptance) should still be bestowed on @RightClick for he pinpointed the definite flaw in the original code (which I, shame on me, at first did not spot...).

collapsar
  • 17,010
  • 4
  • 35
  • 61