SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    UK
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simple javascript application

    Hi, my javascript skills are very limited, so you'll have to forgive my ignorance here!

    I'm trying to build a simple application which lets users enter a number into a form. Upon submission, javascript will check the number and swap an image on the page depending on the number entered.

    e.g. If number entered is <10, swap image to a red image.
    If number between 10 and 20, swap image to amber image.
    If number >20, swap image to green image.


    Is this the kind of thing which can be done with javascript? If so, does anyone have any pointers? I should be able to fine-tune the application myself once I have an idea if it will work/how it works, but any initial help would be greatly appreciated.

    Thanks...

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <img name="SWAP">
    
    <form onsubmit="doSwap(this.number.value); return false">
    	<input name="number"><br>
    	<input type="submit">
    </form>
    
    <script>
    function doSwap(n) {
    	var src = "";
    	
    	if(n < 10)
    		src = "red.gif";
    	else if(n < 20)
    		src = "amber.gif";
    	else
    		src = "green.gif";
    
    	document.images.SWAP.src = src;
    }
    </script>
    Feel free to ask for explanations.

  3. #3
    SitePoint Guru
    Join Date
    Jun 2004
    Location
    UK
    Posts
    605
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, thanks stereofrog! Works a treat.


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
  •