SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: JavaScript Help

  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2002
    Posts
    420
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy JavaScript Help

    I need to make a script that the user puts in their last name and then the program returns who their counsellor is based on the first letter of their name.

    Does any one know how to do this i have yet to find out how.

    Something like:

    Code:
    if (name.firstLetter == a){ 
    teacher = smith
    }
    Wavelan

  2. #2
    SitePoint Addict
    Join Date
    May 2000
    Posts
    313
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is an example of one way you could do it:
    <html>
    <head>
    <title>Teacher Name</title>
    <SCRIPT LANGUAGE="JavaScript">
    // Set up an associative array
    var teacher= new Array()
    teacher["a"]="John Apple"
    teacher["b"]="Mary Lopez"
    teacher["c"]="Don Johnson"

    function Getname(formid)
    {
    // change the form field value to lower case
    var sname=formid.lname.value.toLowerCase()
    // Grab the first letter of the value
    var fletter=sname.substr(0,1)
    // Get the teachers name from the above array
    formid.tname.value=teacher[fletter]
    }
    </SCRIPT>
    </head>
    <body>
    <CENTER>
    <FORM NAME="teacher">
    <INPUT TYPE="text" NAME="lname" SIZE="30">
    <INPUT TYPE="button" VALUE="Get Teacher Name" onClick="Getname(this.form)">
    <BR>
    <INPUT TYPE="text" NAME="tname" SIZE="30">
    </FORM>
    </CENTER>
    </body>
    </html>

  3. #3
    SitePoint Evangelist
    Join Date
    Jan 2002
    Posts
    420
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking Thanks...

    Thanks that worked great!

    One more thing how can i make it so that when you type in the form and press enter it submits it? So you dont have to click on the button just press enter. Is there a way to do this??
    Wavelan

  4. #4
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Not sure if this is what you mean, but:
    <INPUT TYPE="text" NAME="lname" SIZE="30" onBlur='Getname(this.form)'>

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  5. #5
    SitePoint Evangelist
    Join Date
    Jan 2002
    Posts
    420
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    No...

    That didnt do anything?
    What i want though is code that you dont have to click on the submit button instead just press the enter key. Something like on Google after you type in your search query you can just press enter.
    Wavelan

  6. #6
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Sorry about that, I thought you wanted it to activate when the field lost focus.

    Any rate, the following will place the teacher's name as soon as a letter is pressed -- as well as an error message.

    function Getname(formid)
    {
    var sname = formid.lname.value.toLowerCase()
    var fletter=sname.substr(0,1)

    var teach = teacher[fletter]
    if (teach == undefined) teach = 'Sorry no such teacher'
    formid.tname.value = teach;
    }

    <INPUT TYPE="text" NAME="lname" SIZE="30" onKeyUp='Getname(this.form)'>
    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  7. #7
    SitePoint Evangelist
    Join Date
    Jan 2002
    Posts
    420
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question ?

    OK so say I wanted to use a SWITCH statement, or something other than an array, for this what would just the one line of code be for getting the first letter of the input ?

    Something Like:

    Code:
    var lastName = prompt('Enter your last name');
    var firstLetter = lastName.firstletter();
    Wavelan

  8. #8
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var firstLetter = prompt('Enter your last name', '').charAt(0);
    ck :: bringing chris to the masses.

  9. #9
    SitePoint Evangelist
    Join Date
    Jan 2002
    Posts
    420
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks

    Thanks for all your help!
    Wavelan


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •