Forum Moderators: open

Message Too Old, No Replies

Text Swap (A to B, B to A)

Im trying to make a text swap with an img onclick javascript function.

         

nesohc

11:12 am on Oct 11, 2009 (gmt 0)

10+ Year Member



Hey! Im new to this forum and kinda new to web-development. Im atm trying to make an onClick on an image, which should swap a text like this: A to B and it should swap between A and B so A to B and B to A every time you click.

I also have an okey button, this is made in php and mysql, I need this button to be able to keep track of which one is the top and bottom on in.

The way I've tried it is by making 4 <p> tags and in my onClick javascript changing the visibility on the tags. This does not seem to be working, nor do I know how I could make php to keep track of it meantime. Thats why I posted in Ajax because I've heard Ajax might be of assistance here. Another problem I seem to have is my safari browser does not react to my onClick on the image, works well in Opera but Safari only reacts on MouseOver.

Example

A
to
B

swap to

B
to
A

brotherhood of LAN

12:08 pm on Oct 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to the forums nesohc

I saved this on my desktop and it worked fine in Firefox

<img width="10" height="10" onclick="if(document.getElementById('changethis').innerHTML == 'A') document.getElementById('changethis').innerHTML = 'B'; else document.getElementById('changethis').innerHTML = 'A';" />

<p id="changethis">A</p>

If you wanted to send A/B to PHP/MySQL, you could use the javascript document.getElementById.innerHTML via AJAX.

IF you don't already have a working AJAX script, they are readily available with a quick search.

rocknbil

5:57 pm on Oct 11, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- doctype on one line please -->
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Untitled</title>
</head>
<body>
<form action="" onSubmit="return swap('changeme');">
<input type="submit" value="Click to Change">
<p id="changeme">A</p>
</form>
<script type="text/javascript">
function swap(el) {
if (document.getElementById(el)) {
var obj = document.getElementById(el);
obj.innerHTML=(obj.innerHTML=='A')?'B':'A';
}
return false;
}
</script>
</body>
</html>

nesohc

6:14 pm on Oct 11, 2009 (gmt 0)

10+ Year Member



okey, thanks guys. Really quick responses and ty alot :)

But how do I continue this, I mean I need to be able to push my okey button as well and know which of A and B is the top one in php?