View Full Version : help me with the simple question

05-05-2006, 03:27 PM
original text is
More text

if user clicks text above, it changes to
Less text

if user clicks the changed text, it recovers to original text, and so on...

but the change will not affect other part of html page.


05-05-2006, 04:33 PM
you need an element, let's make a span:

<span>More Text</span>

Now, you will be operating on this span. So you want an onclick. We'll do this inline since it's simple. Be aware that there are many ways to do the same thing:

<script type="text/javascript">
function swapText(p_element, p_text)
if (p_element.altText)
{ // *** If the element has already has its text swapped ***
var altText = p_element.altText; // *** Get the alternate text ***
p_element.altText = p_element.innerHTML; // *** Save the current text ***
p_element.innerHTML = altText; // *** Display the alternate text ***
{ // *** The element has not yet had its text swapped ***
p_element.altText = p_element.innerHTML; // *** Save the current text ***
p_element.innerHTML = p_text; // *** display the text passed into this function ***

<span onclick="swapText(this, 'Less Text');">More Text</span>

Lots of ways to do this. Hope this one helps.

05-06-2006, 12:14 PM
it works perfectly.

:thumbsup: :thumbsup: :thumbsup: :thumbsup: :thumbsup:
:D :D :D :D :D

05-06-2006, 01:43 PM
Much more easier with RegExp:

<script type="text/javascript">
function swapText(txt){
<span onclick="swapText(this.firstChild)">More Text</span>