Change the text between the <a> tag

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
kendall
Forum Regular
Posts: 852
Joined: Tue Jul 30, 2002 10:21 am
Location: Trinidad, West Indies
Contact:

Change the text between the <a> tag

Post by kendall »

Hey guys,

Do any of you guys have a universal way to change the text between the "<a>" link tags?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Untested

Code: Select all

<a href="foo.php" id="foo">Default text</a>
<script type="text/javascript">
<!--

if (document.getElementById('foo')) document.getElementById('foo').innerText = 'New text';

//or (can't quite remember)

document.getElementById('foo').text = 'New text';

// -->
</script>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'd suggest using innerHTML instead of innerText, which from what I remember, isn't supported on a few browsers..
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Code: Select all

function writit(text,id) {
	if (document.getElementById) {
		x = document.getElementById(id);
		x.innerHTML = '';
		x.innerHTML = text;
	} else if (document.all) {
		x = document.all[id];
		x.innerHTML = text;
	} else if (document.layers)	{
		x = document.layers[id];
		text2 = '<P CLASS="testclass">' + text + '</P>';
		x.document.open();
		x.document.write(text2);
		x.document.close();
	}
}
Post Reply