11 Replies - 674 Views - Last Post: 16 November 2012 - 02:08 AM

#1 Eggy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 15-November 12

Javascript switch statements?

Posted 15 November 2012 - 05:35 PM

 
<!DOCTYPE html>
<html>
<body>

<p>Type in the day of the week in the text box (numbers 1 to 7) and click 'search'</p>

<input type="text" id="dayInput" /> 
<button onclick="myFunction()">search</button>

<p id="demo"></p>

<script>
function myFunction()
{
var x;
var d = document.getElementById('dayInput).value; 
switch (d)
  {
  case 0:
    x="Lectures ... Sunday";
    break;

  case 1:
    x="Lectures... Mon";
    break;
  
  case 2:
    x="Lectures... Wed";
    break;
  
  case 3:
    x="Lectures... Thu";
    break;
  
  case 4:
    x="Lectures...Friday";
    break;
  
  case 5:
    x="Lectures...Fri";
    break;
  
  case 6:
    x="Lectures..Sat";
    break;
  
  }
  
document.getElementById("demo").innerHTML=x;
}

</script>

</body>
</html>






I type in a number in the search box (say 2) and the output is 'undefined'?

I don't understand why it's not giving me the case output.

I've tried to alter this code: http://www.w3schools...s/js_switch.asp

I can honestly say I've been stuck on this all day : / and just can't get it to work. Don't give me the code, just tell me what I'm doing wrong.

I've actually never done Javascript before (and yes this is a college assignment! A user enters a day of the week (number) and when he/she clicks search, we output the lectures we have on that particular day)

Thank you for your help.

Is This A Good Question/Topic? 0
  • +

Replies To: Javascript switch statements?

#2 IJET  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 93
  • View blog
  • Posts: 121
  • Joined: 14-October 12

Re: Javascript switch statements?

Posted 15 November 2012 - 06:08 PM

Well, it's always nice to see all of the code to test things, but the first thing I noticed is this:

getElementById('dayInput).value; 


Should have an end '. Example: 'dayInput'

Edit: Okay, upon further looking at your code instead of just skipping to just the JS, you'll also need to parse that text that gets entered. Should work then.

This post has been edited by IJET: 15 November 2012 - 06:18 PM

Was This Post Helpful? 0
  • +
  • -

#3 Eggy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 15-November 12

Re: Javascript switch statements?

Posted 15 November 2012 - 06:19 PM

View PostIJET, on 15 November 2012 - 06:08 PM, said:

Well, it's always nice to see all of the code to test things, but the first thing I noticed is this:

getElementById('dayInput).value; 


Should have an end '. Example: 'dayInput'


Hi, thank you for your reply.

However, I have no idea what you're talking about.

(Sorry!)

Oh, I missed the apostrophe
Was This Post Helpful? 0
  • +
  • -

#4 IJET  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 93
  • View blog
  • Posts: 121
  • Joined: 14-October 12

Re: Javascript switch statements?

Posted 15 November 2012 - 06:19 PM

I'll just make a new reply. Do you see how it says 'dayInput? It needs to be 'dayInput(') <--- notice that is missing in your original code. You also need to parse your input value to integer.
Was This Post Helpful? 0
  • +
  • -

#5 Eggy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 15-November 12

Re: Javascript switch statements?

Posted 15 November 2012 - 06:20 PM

View PostEggy, on 15 November 2012 - 06:19 PM, said:

View PostIJET, on 15 November 2012 - 06:08 PM, said:

Well, it's always nice to see all of the code to test things, but the first thing I noticed is this:

getElementById('dayInput).value; 


Should have an end '. Example: 'dayInput'


Hi, thank you for your reply.

However, I have no idea what you're talking about.

(Sorry!)

EDIT:

Oh, I missed the apostrophe


I still can't get it to work.
Was This Post Helpful? 0
  • +
  • -

#6 IJET  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 93
  • View blog
  • Posts: 121
  • Joined: 14-October 12

Re: Javascript switch statements?

Posted 15 November 2012 - 06:21 PM

Okay, try this.

var x;
var d = document.getElementById('dayInput').value;
var y = parseInt(d);
switch (y)


Was This Post Helpful? 1
  • +
  • -

#7 Eggy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 15-November 12

Re: Javascript switch statements?

Posted 15 November 2012 - 06:23 PM

View PostIJET, on 15 November 2012 - 06:21 PM, said:

Okay, try this.

var x;
var d = document.getElementById('dayInput').value;
var y = parseInt(d);
switch (y)



It worked!! Thank you very much!
Was This Post Helpful? 0
  • +
  • -

#8 IJET  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 93
  • View blog
  • Posts: 121
  • Joined: 14-October 12

Re: Javascript switch statements?

Posted 15 November 2012 - 06:25 PM

Excellent. Understand that the input type in the HTML is "text". When it takes the value, it becomes a string. You must use parseInt on a string to convert it to an integer/readable number.
Was This Post Helpful? 1
  • +
  • -

#9 Eggy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 15-November 12

Re: Javascript switch statements?

Posted 15 November 2012 - 06:33 PM

View PostIJET, on 15 November 2012 - 06:25 PM, said:

Excellent. Understand that the input type in the HTML is "text". When it takes the value, it becomes a string. You must use parseInt on a string to convert it to an integer/readable number.


Right, understood. I have never come across parselnt before. Thank you!
Was This Post Helpful? 0
  • +
  • -

#10 IJET  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 93
  • View blog
  • Posts: 121
  • Joined: 14-October 12

Re: Javascript switch statements?

Posted 15 November 2012 - 06:39 PM

You're welcome. You've learned something new today then, which is always a good thing! Good luck.
Was This Post Helpful? 0
  • +
  • -

#11 Sho Ke  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 110
  • View blog
  • Posts: 250
  • Joined: 13-October 11

Re: Javascript switch statements?

Posted 15 November 2012 - 08:38 PM

View PostIJET, on 16 November 2012 - 01:21 AM, said:

Okay, try this.

var x;
var d = document.getElementById('dayInput').value;
var y = parseInt(d);
switch (y)


For future reference, this is a waste of resources, using a dummy variable like that when it's completely unnecessary.
Instead of going through the hassle instantiating y and consuming the additional resources, just parse dayInput's value as int directly:
var d = parseInt(document.getElementById('dayInput').value);



This post has been edited by Sho Ke: 15 November 2012 - 08:39 PM

Was This Post Helpful? 0
  • +
  • -

#12 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3480
  • View blog
  • Posts: 10,028
  • Joined: 08-June 10

Re: Javascript switch statements?

Posted 16 November 2012 - 02:08 AM

additional explanation on switch(): switch() does an equality test (testing for value and type), so 2 !== "2". be aware that there are other "comparison" functions, that do that too (e.g. Array.indexOf()).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1