...

View Full Version : help me with the simple question



hello20109876
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.

thx

Beagle
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 ***
}
else
{ // *** 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 ***
}
}
</script>

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


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

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

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

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



<script type="text/javascript">
function swapText(txt){
txt.data=/More/.test(txt.data)?txt.data.replace(/More/,'Less'):txt.data.replace(/Less/,'More');
}
</script>
...
<span onclick="swapText(this.firstChild)">More Text</span>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum