JavaScript Javascript switch wrong case

Hello (yet again) a question about javascript, this one has to be an easy one but I just can't see what I did wrong, I think something really stupid:

I have a textbox and a button. When i click the button the value wil be passed to a variable called activiteitn. Code:

    activiteitn = $("#m2activiteitn").val();

It gets the value, so when I filled in the number 1, activiteitn = 1. After that I have a switch that looks like this:

        switch(activiteitn) {
        case  1: activiteitn = 1.2;
        break;
        case  2: activiteitn = 1.375;
        break;
        case  3: activiteitn = 1.55;
        break;
        case  4: activiteitn = 1.725;
        break;
        case  5: activiteitn = 1.9;
        break;
        default: alert("hoi");
        break;
        }

The problem is that even if I fill in the number 1, it jumps to the default case, causing it to alert. If I place a alert at case 1 that alert does not show up. The first thing that came in my mind was that the activiteitn was not 1, so right after the switch I placed another alert:

        alert (activiteitn);

Now, first what happends is I get the alert "hoi", then I get the second alert of activiteitn that says: 1. So after that I thought, "Maybe its a string", so I changed the cases to strings like: case "1" & case ("1") but that did not work either. The problem, it always goes to the default case and I can not figure out why that is, I hope someone sees what I did wrong and can help me out.

Answer:1

.val() returns a string, not a number. So after the line:

activiteitn = $("#m2activiteitn").val();

the variable activiteitn actually equals "1", not 1. Since case...switch uses strict equality, your test will always fail.

To fix, simply change the above line to:

activiteitn = +$("#m2activiteitn").val(); // convert to a number

or

activiteitn = parseInt($("#m2activiteitn").val(),10); // convert to an integer

or

activiteitn = parseFloat($("#m2activiteitn").val()); // convert to a float
Answer:2

var prompt = ... var connection = ... prompt.start(); var property = { name: 'yesno', message: 'approve this screencast?', validator: /y[es]*|n[o]?/, warning: 'Must respond yes or no', ...

var prompt = ... var connection = ... prompt.start(); var property = { name: 'yesno', message: 'approve this screencast?', validator: /y[es]*|n[o]?/, warning: 'Must respond yes or no', ...

I converted my CoffeeScript code to JavaScript using http://js2coffee.org ResetControls = -> $("#menu_item_url").val "" $("#menu_item_name").val "" $("#resource_id").prop "selectedIndex", 0 ...

I converted my CoffeeScript code to JavaScript using http://js2coffee.org ResetControls = -> $("#menu_item_url").val "" $("#menu_item_name").val "" $("#resource_id").prop "selectedIndex", 0 ...

  1. javascript function assigned to variable
  2. javascript call function assigned to variable
  3. javascript assign function with parameters

JQuery is not detected in my app Django : I have a html page links.html : <link rel="stylesheet" type="text/css" href="/static/css/mysheet.css"> <script type="text/javascript" src="/static/...

JQuery is not detected in my app Django : I have a html page links.html : <link rel="stylesheet" type="text/css" href="/static/css/mysheet.css"> <script type="text/javascript" src="/static/...

  1. using jquery with django
  2. jquery ajax with django
  3. jquery autocomplete with django
  4. jquery datatable with django
  5. jquery datepicker with django
  6. jquery ui with django
  7. jquery django template
  8. jquery django admin
  9. jquery django csrf token
  10. jquery django rest framework
  11. jquery django formset
  12. jquery django example
  13. jquery django post
  14. jquery django form
  15. jquery django tutorial
  16. jquery django url
  17. jquery django csrf
  18. jquery django variable
  19. jquery django install
  20. jquery django post request

How can I check inut field's of the type file (size, ending(jpg, png, ...))? On my page you can add more input field's of the type file if you press on the button "Schritt hizuf├╝gen". Here is jquery ...

How can I check inut field's of the type file (size, ending(jpg, png, ...))? On my page you can add more input field's of the type file if you press on the button "Schritt hizuf├╝gen". Here is jquery ...

  1. check input type python
  2. check input type javascript
  3. check input type jquery
  4. check input type java
  5. check input type c++
  6. check input type file empty jquery
  7. check input type checkbox jquery
  8. check input type file empty php
  9. check input type checkbox javascript
  10. check input type php
  11. check input type email jquery
  12. check input type radio checked jquery
  13. check input type file empty javascript
  14. check input type radio checked javascript
  15. check input type file has value