Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

if then else and elseif help needed

Status
Not open for further replies.

OnLiner

Technical User
Dec 4, 2002
87
US
Hi:)

I work as a dispatcher and want to create a simple form in our intranet home page like so....

input type=text --> in this box i enter the number of a cars route, and then press the enter key.

input type=text --> the second box shows the phone extension of the dispatcher that is handling the entered route above.

So if I enter route "231" in the first box and hit the enter key, the second box will show "4050", which is the extension of the dispatcher that is handling that route.

I am assuming I can use the if-then-else with elseif functions. Can someone give me a hint on how to put this together or better yet post a sample?

I greatly appreciate it.
 
I' d use a case statement instead of if elses.It's much cleaner and easier to write.
pseudo code;
Code:
<script>
function DispExt(){
x = document.getElementByID("carRoute")
y = document.getElementByID("extention")
  switch (x) 
  {
    case 1:
    y.value = 200
     break
    case 2:
    y.value = 300
    break
    case 3:
    y.value = 400
    break
    case 231:
    y.value = 4050
    break
........
  }
}
</script>
<input type = "text" id = "carRoute" onblur = "DispExt()">
<input type = "text" id = "extention">
think this is close untested though.






Glen
 
If you have a lot of route numbers, I'd use an associative array instead:
Code:
<html>
<head>
<script>
var lookup=new Array();
lookup["231"]="4050";
lookup["456"]="4051";
lookup["789"]="4052";

function doLookup(t){
  if(event.keyCode == 13){
    if(typeof lookup[document.f.t1.value]=="undefined"){
      document.f.t1.value='';
      alert('Route not found!');
    }else{
      document.f.t2.value = lookup[document.f.t1.value];
    }
    return false;
  }
  return true;
}
</script>
</head>
<body>
<form name="f">
  <input type="text" name="t1" onkeypress="return doLookup()"><br>
  <input type="text" name="t2">
</form>
</body>
</html>

Adam

There are only 10 types of people in the world: Those who understand binary, and those who don't
 
After a little playing I feel bad about my last post this is closer I'd say
Code:
<script>
function DispExt(){
var x = document.getElementById('carRoute').value;
 switch (x) 
  {
    case '1':
 document.getElementById('extention').value = '100'
    break
   case '2':
   document.getElementById('extention').value ='300'
    break
    case '3':
    document.getElementById('extention').value = '400'
    break
    case '231':
    document.getElementById('extention').value = '4050'
    break
default:
  alert("Wrong  Route number\n Try Again ")
 document.getElementById('carRoute').value = ''
 document.getElementById('carRoute').focus()
break
  }
}
</script>
<input type = "text" id = "carRoute" onblur = "DispExt()">
<input type = "text" id = "extention">
Adams post is probably better though.

Glen
 
Hi!

One more quick question. What would I have to change in the code if I wanted more than one of these in a web page, I used the array example.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top