Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Javascript Homework Help

javascript button input animation

  • Please log in to reply
4 replies to this topic

#1 AshleyTruax

AshleyTruax

    CC Lurker

  • New Member
  • Pip
  • 2 posts

Posted 19 February 2013 - 03:50 PM

I am having issues trying to get my javascript code to work. I wanted to use both buttons and input text to play an animation. The buttons work, but I can get the animations to play well with the user's input text. It only plays one specific animation. Any ideas on what can help? I tried using return value to try to get it to look for the user input letter and play the animation based on the letter. Thanks for any help.

 

 

function nameTheRock() {
 //user alert to decide the name for the pet
 //var namePet=prompt("Please enter your pet name: ", "your name here");
 //shows the user the name they choose for thier pet rock
 //user alert to decide the name for the pet
 var namePet=prompt("Please enter your pet name: ", "your name here");
 //doesn't allow the user not
 if(namePet != null && namePet!=""){
      form.petName.value=namePet;
 }
 //checks for errors to ensure user puts in a name
    else{
    alert("Please name your pet");    
  }
}

//user key events that allows the user to enter keys j, h, s, b, and g to play animations
  var userCode = document.getElementById("hiddenText").value;
  userCode = userCode.toLowerCase();
  text = {};
  text.command = userCode;
function userKey() {
         //creates variables that containes windows and other browser capability
        if (userCode ="j") {
            jumps();
        }
         else if (userCode ="g") {
              glows();
        }
         else if (userCode ="h") {
          hops();
        }
         else if (userCode ="b") {
              blinks();
        }
         else if (userCode ="s") {
               smiles();
        }
        else {
            alert("Please use either a button or a letter to play the animation");
        }
    }

function jumps() {     
    //When the user click the button it will change the image to an
animated gif. This is done by changing the image source to another image
that contains the animations.
    document.getElementById('image').src= "jump.gif";
}


function glows() {     
     //When the user click the button it will change the image to an animated gif.
    document.getElementById('image').src= "glow.gif";     
}

function hops() {     
    //When the user click the button it will change the image to an animated gif.
    document.getElementById('image').src= "hop.gif";      
}

function smiles() {     
    //When the user click the button it will change the image to an animated gif.
    document.getElementById('image').src= "smiles.gif";      
}

function blinks() {     
    //When the user click the button it will change the image to an animated gif.
    document.getElementById('image').src= "blink.gif";      
}

 


Edited by Roger, 19 February 2013 - 04:22 PM.
Added code tags

  • 0

#2 BenW

BenW

    CC Devotee

  • Community Manager
  • 976 posts
  • Programming Language:ActionScript
  • Learning:ActionScript

Posted 19 February 2013 - 06:17 PM

You need to use a double = sign when you're comparing values, like this:

 

if (JustinBieber == "male"){
girlyhaircut()
}

 

Go through your userKey function and fix the if conditions in that, and then see if the problem is solved :)


  • 1

fss_overall.png

 


#3 rodeone2

rodeone2

    CC Lurker

  • New Member
  • Pip
  • 4 posts
  • Programming Language:C, PHP, JavaScript

Posted 19 February 2013 - 07:26 PM

function userKey($val='h') //PLACE ($uid) for production //testing value only
 {
 
  //$role=2; user roles function  
  
if ($val == 'j') {
    jumps();
} 
if ($val == 'g') {
    glows();
	}
	
if ($val == 'h') {
    hops();
	}	
	
if ($val == 'b') {    //advised to add global $admin here
    smiles();
	}	
	
if ($val == 's') {
	jumps();
	}
if ($val == '') {
    echo error();
	}	

} // end function userKey()

  • 0

#4 AshleyTruax

AshleyTruax

    CC Lurker

  • New Member
  • Pip
  • 2 posts

Posted 20 February 2013 - 08:47 AM

You need to use a double = sign when you're comparing values, like this:

 

 

if (JustinBieber == "male"){
girlyhaircut()
}

 

Go through your userKey function and fix the if conditions in that, and then see if the problem is solved :)

I tried it with the double quates but it doesn't see the text at all and just goes straight to the else statement.


  • 0

#5 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts
  • Location:Argentina
  • Programming Language:C, C++, C#, PHP, JavaScript, Transact-SQL, Bash, Others
  • Learning:Java, Others

Posted 20 February 2013 - 06:25 PM

I think what you need is a switch statement instead of a series of ifs

 

 
function userKey()
{
        
switch(userCode)

{

case "j" :      jumps();   break;

case "g":      glows();    break;
 
case  "h":   hops(); break;
 
case "b":   blinks();  break;          
 
case "s" :     smiles();  break;                    
    
default: alert("Please use either a button or a letter to play the animation"); break;
 
}
}

  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download