Question

I am feeling a bit embarrassed to ask this silly question but somehow the following code is not working. The values (bed,room,floor) are fetched right but switch case not working.But the same snippet of switch case code works well in jsfiddle.I m sure I m missing something silly.I m using this in socialengine with mootools enabled

I also want to know how to get an element which has id=roomtype and which is inside a div whose class=form-elements but id=roomtype is not direct child of div class=form-elements. I can get it by $$('.form-elements #roomtype').get('value') but this refers all elements with #roomtype, this $('.form-elements #roomtype').get('value') doesnt work

<script type="text/javascript">
var updatePrice=function()
{

 var room= $$('.form-elements #roomtype').get('value')  ;

// alert (room) gets AirCon or Dorm
var price;

switch (room)
  {
  case "AirCon":
    price="10000"; alert("AirCon");
    break;
  case 'Dorm':
    price="5000"; alert("Dorm");
    break;
  default:
    price="00";
  }


}

 en4.core.runonce.add(updatePrice);// this add this function to DOM Ready
    </script>
Was it helpful?

Solution

#1

ID's must be unique.

In MooTools you can do $('roomtype') to get the element with the ID roomtype.

You can also use document.id('roomtype') or even $$('#roomtype').
Since ID's must be unique it's irrelevant the parent of that element, because there is only one.

#2

Note that $ and $$ are different element methods in mootools. $$ takes a CSS selector and returns a collection/array.

$$('#roomtype').get('value'); // returns ['the value']
$('roomtype').get('value'); // return 'the value'

#3

The other problem I see is that you define the price inside the scope of updatePrice() function, so doing price = 5000 inside the switch will not leave the function's scope anyway. You might want to define the var price outside the function, or make the function return that value lie this:

    // ...
    switch (room) {
        case "AirCon":
            price = "10000";
            alert("AirCon");
            break;
        case 'Dorm':
            price = "5000";
            alert("Dorm");
            break;
        default:
            price = "00";
    }
    return price; // here
}

If you update your fiddle with HTML it will be easier to help you more.

P.s: You are always welcome to ask questions. No need to feel embarrassed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top