Web Analytics Made Easy -
StatCounter Help for newbie! - CodingForum

Announcement

Collapse
No announcement yet.

Help for newbie!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help for newbie!

    Hi all

    I haven't done any programming since my college days in the late 80s, so I have little knowledge of HTML and Javascript, so this probably simple task is frustrating me...

    I am trying to code a simple html web page that asks one question "What is your unique ID?". They will be given a 10 character ID via an invitation letter. The first letter will be A, B, C, or D. Dependant on that letter (first character in the unique ID), I want it to go to a specific survey web page when the either press ENTER or click the SUBMIT button. There are 4 different surveys (A, B, C, D) which will have unique URLs.

    Thats it basically! It didnt seem too difficult to do, but I cant get it to work

    Any advice with a small piece of code would be superb!

    Many thanks

  • #2
    We normally ask you to show us your code and we show you the mistakes and correct them for you, but this looks like a one time affair so here:
    Code:
    <!DOCTYPE html>
    <html>
    <head>
    <title>New document</title>
    
    </head>
    <body>
    What is your unique ID?<input type="text" id="inp" style="margin-left:15px;" autofocus="autofocus">
    <button onclick="tonext()">Submit</button>
    
    <script >
    function tonext(){
    	var str = document.getElementById("inp").value;
    	var char = str.charAt(0);
    	switch(char) {
        case 'a':
        case 'A':
            window.location.href = 'https://www.google.com/';
            break;
        case 'b':
        case 'B':
            window.location.href = 'https://www.google.com/';
            break;
    	case 'C':
    	case 'c':
            window.location.href = 'https://www.google.com/';
            break;
    	case 'D':
    	case 'd':
            window.location.href = 'https://www.google.com/';
            break;
        default:
            window.location.href = 'https://www.google.com/';
    	}
    }
    </script>
    </body>
    </html>
    All transfers go to google; use yours here instead.
    Evolution - The non-random survival of random variants.
    Physics is actually atoms trying to understand themselves.

    Comment


    • #3
      Thats fantastic sunfighter! Many many thanks! :-)

      Comment


      • #4
        Instead of

        Code:
        	var char = str.charAt(0);
        	switch(char) {
            case 'a':
            case 'A':
                window.location.href = 'https://www.google.com/';
                break;
            case 'b':
            case 'B':
                window.location.href = 'https://www.google.com/';
                break;
        	case 'C':
        	case 'c':
                window.location.href = 'https://www.google.com/';
                break;
        	case 'D':
        	case 'd':
                window.location.href = 'https://www.google.com/';
                break;
            default:
                window.location.href = 'https://www.google.com/';
        	}
        you can get rid of half the case statements like this:

        Code:
        	var char = str.charAt(0);
        	switch(char.toUpperCase()) {
            case 'A':
                window.location.href = 'https://www.google.com/';
                break;
            case 'B':
                window.location.href = 'https://www.google.com/';
                break;
        	case 'C':
                window.location.href = 'https://www.google.com/';
                break;
        	case 'D':
                window.location.href = 'https://www.google.com/';
                break;
            default:
                window.location.href = 'https://www.google.com/';
        	}
        Stephen
        Learn Modern JavaScript - http://javascriptexample.net/
        Helping others to solve their computer problem at http://www.felgall.com/

        Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

        Comment

        Working...
        X