473,395 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Swapping between images

How can i swap between two images each time the user clicks. The user can should only be allowed to click 49 times.
Jan 15 '07 #1
6 1232
acoder
16,027 Expert Mod 8TB
When the user clicks on what? The image?

Yes, that is possible using javascript.

Change the source of the image, e.g.
Expand|Select|Wrap|Line Numbers
  1. img.src = 'new_image.gif;
Jan 15 '07 #2
Apologies my above post was ambigious. What i meant is that i need to create two players. One player is assigned one image say red and the other is assigned another called yellow.

There are 49 different white images. How can alternate between the two players so when player 1 clicks an image it turns to red and when player 2 clicks an image it turns to yellow?
Jan 15 '07 #3
acoder
16,027 Expert Mod 8TB
There are 49 different white images. How can alternate between the two players so when player 1 clicks an image it turns to red and when player 2 clicks an image it turns to yellow?
In the function which changes the image, just change the colour to red or yellow. Declare a global variable, say col which you can use for the current colour. There are other ways, but this is one way.
Jan 15 '07 #4
This is the code so far but every time i click it still shows a red counter.

Expand|Select|Wrap|Line Numbers
  1. var whoseTurn = 1
  2.  
  3.    playerChecker = new Image()
  4.    playerChecker.src = "redCounter.jpg"
  5.  
  6.    if(whoseTurn == 1){
  7.    playerChecker.src = "redCounter.jpg"
  8.    } else if(whoseTurn == 2) {
  9.    playerChecker.src = "yellowCounter.jpg"
  10.    } 
  11.  
  12.    function switchTurns(){
  13.        if(whoseTurn == 1){
  14.        whoseTurn = 2;
  15.    } else {
  16.        whoseTurn = 1;
  17.    }}
  18.  
  19.    function changeimage(id){
  20.        switchTurns()    
  21.        if(document.getElementById([id]).src == "whiteCounter.jpg"){
  22.            document.getElementById([id]).src = playerChecker.src ;          
  23.        }}
Jan 16 '07 #5
acoder
16,027 Expert Mod 8TB
This is the code so far but every time i click it still shows a red counter.

Expand|Select|Wrap|Line Numbers
  1. var whoseTurn = 1
  2.  
  3.    playerChecker = new Image()
  4.    playerChecker.src = "redCounter.jpg"
  5.  
  6.    if(whoseTurn == 1){
  7.    playerChecker.src = "redCounter.jpg"
  8.    } else if(whoseTurn == 2) {
  9.    playerChecker.src = "yellowCounter.jpg"
  10.    } 
  11.  
  12.    function switchTurns(){
  13.        if(whoseTurn == 1){
  14.        whoseTurn = 2;
  15.    } else {
  16.        whoseTurn = 1;
  17.    }}
  18.  
  19.    function changeimage(id){
  20.        switchTurns()    
  21.        if(document.getElementById([id]).src == "whiteCounter.jpg"){
  22.            document.getElementById([id]).src = playerChecker.src ;          
  23.        }}
There are a few problems with the code. The code for switching the colour must be included in the switchTurns function:
Expand|Select|Wrap|Line Numbers
  1. function switchTurns() {
  2.  if (whoseTurn == 1) {
  3.   whoseTurn = 2;
  4.   playerChecker.src = "yellowCounter.jpg"
  5.  } else {
  6.   whoseTurn = 1;
  7.   playerChecker.src = "redCounter.jpg"
  8.  }
  9. }
  10.  
Alternatively, keep switchTurns as it is and put your image swapping code in another function and call that in switchTurns (at the end) or in changeimage.

In changeimage, the id should not have square brackets, e.g.
Expand|Select|Wrap|Line Numbers
  1. document.getElementById(id).src
Jan 16 '07 #6
That now works. Thanks allot you have been a great help.
Jan 16 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Richard | last post by:
I'm beginning to undertake php for the fun of it. Working on a problem I hope can be solved in php. Basically what I have is this: <body> <img name="main" src="image1.jpg"> <a href="#"...
2
by: uNConVeNtiOnAL | last post by:
Hi - I have a page that has a button that makes a prompt appear for the user to type input. When they have typed in input, I want the button to change color when the prompt closes. I am...
1
by: Yon | last post by:
I've been trying to figure out how do i swap images that uploaded onto an object. I want to leave the object as it original form but the images that are uploaded are placed on the object.
1
by: Terry Haufler | last post by:
I am trying to swap/rotate random flash banners using the following Javascript code. I have 3 flash headers/banners. I can get it to open a page with a random header using...
7
by: Jon Glazer | last post by:
I know this is sorta reposting but I want to simplify the issue a bit. Take the following script: function replacemain(s){ var x = document.images; var y = document.images; var pictemp =...
6
by: cjl | last post by:
I have a website which allows people to view interesting radiology cases. It is for teaching radiology residents. It is designed to run in fullscreen or 'kiosk' mode, at 1024 x 768 resolution...
2
by: TuftyTrue | last post by:
Hi, On my site the user can browse to an image with <input type=file etc>. Althrough I have no problem swapping images with: document.getElementById('PictureToDisplay').src =...
1
by: SipherDrakon | last post by:
I know I saw a similar posting about this but mine is a little more complicated and being barely able to understand javascript, i am having a little trouble. <FORM...
3
by: Geoff Cox | last post by:
Hello, Way back in 2000 Ken Cox put up this code for swapping images on a button. I cannot work out how to use it so that more than 1 button can use it. I guess the problem is knowing how to...
1
by: Prisoner at War | last post by:
Hi, Everybody, The fucntion below is called by an onLoad <imgattribute, and it is supposed to replace the original image, after three seconds, with another image. This second image shouldl be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.