Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
96
rated 0 times [  101] [ 5]  / answers: 1 / hits: 43555  / 12 Years ago, mon, august 27, 2012, 12:00:00

I am trying to change the text of a link on click and then change back again to the original when clicked again. I am able to change the text, (from READ MORE... to CLOSE) but have been fighting to change the text back again (from CLOSE to READ MORE...)



 <script language=javascript>
function changeText(idElement){
if(idElement==1){
document.getElementById('element'+idElement).innerHTML ='Close';
} else if(idElement==2){
document.getElementById('element'+idElement).innerHTML ='Close';
}
}
</script>

<a id=element1 onClick=javascript:changeText(1)>Read More...</a>
<a id=element2 onClick=javascript:changeText(2)>Read More...</a>


Thanks for any help in advance.


More From » text

 Answers
50

This should work:



function changeText(idElement) {
var element = document.getElementById('element' + idElement);
if (idElement === 1 || idElement === 2) {
if (element.innerHTML === 'Read More...') element.innerHTML = 'Close';
else {
element.innerHTML = 'Read More...';
}
}
}


Demo: http://jsfiddle.net/UfVAH/


[#83398] Sunday, August 26, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
minab

Total Points: 701
Total Questions: 104
Total Answers: 91

Location: Saint Pierre and Miquelon
Member since Fri, Jan 28, 2022
2 Years ago
;