how to dynamically manipulate icons using javascript

J

jayanthigk2004

Folks,

I need a working code to do the following or something similar.

I have two icons in the screen. One to indicate right (a tick mark) and
one to indicate wrong (a cross).

If the user clicks on the first icon it should become tick and the
second one should become cross.

If the user clicks on the second icon then it should become tick and
the first one should become cross.

all this must happen in client side (no post back).

This is not my actual requirement but a small part of it. Once I have a
prototype code to do this I will build the rest from there.

Thanks
 
A

ASM

(e-mail address removed) a écrit :
Folks,

I need a working code to do the following or something similar.

I have two icons in the screen. One to indicate right (a tick mark) and
one to indicate wrong (a cross).

click what icon you want exchanges both images

<img src="ok.jpg" alt="" name="ok"
onclick="
var icon = this.src;
this.src = document.KO.src;
document.KO.src = icon;
">
<img src="X.jpg" alt="" name="KO"
onclick="
var icon = this.src;
this.src = document.ok.src;
document.ok.src = icon;
">


I would prefer :

one image (icons.jpg) with both icons

+-----+-----+
| | |
| O K | X |
| | |
+-----+-----+

CSS :
=====

..icon { cursor: pointer; }
..icon span { background: url(icons.jpg) no-repeat left center; }
..icon span.ko { background-position: right center; }

JS :
=====

function icon(what) {
what = what.getElementsByTagName('SPAN')[0];
what.className = what.className==''? 'ko' : '';
}

HTML :
======

<p class="icon" onclik="icon(this);">
<span>&nbsp; &nbsp;</span>Question 1</p>
<p class="icon" onclik="icon(this);">
<span>&nbsp; &nbsp;</span>Question 2</p>
<p class="icon" onclik="icon(this);">
<span>&nbsp; &nbsp;</span>Question 3</p>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top