Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Determining the 'value' of a link?

Status
Not open for further replies.

LucyL

Technical User
Feb 20, 2002
113
0
0
US
Hi,
At the moment I have a button which shows/hides a table. However I want to show/hide the table using a link because it looks neater on the page.

What I want to do is this, but am not suceeding:
The link will have 2 states - SHOW and HIDE.
When I click the link I want to call a function which determines the value of the link i.e Show or Hide.
If the value = Show, call function which displays the table.
If the value = Hide, call function which hides the table.

I am not able to retrieve the value of the link. I am getting an error saying it is null or not an object. Is a link an object?

(My form is called form1)
Here is my link:
<a name="linkName" id="linkName" href="#" onClick="alertValueNow();">Show</a>

Here is my JavaScript function:
function alertValueNow()
{
var name = document.form1.linkName.value;
alert("Hello " + name)
}

The error is telling me that document.form1.linkName.value is null or not an object.

Is it possible to pull the 'Show' value out from the link - or else how do I determine which state (show or hide) the link is in?


Thank you



 

Lucy,

Try something like this:

Code:
<html>
<head>
<script type="text/javascript">
<!--
	function toggleTable(hrefObj)
	{
		if (hrefObj.innerText.indexOf('Show') != -1) {
			// show table and change link text to read "hide"
			hrefObj.innerText = 'Hide table';
			document.getElementById('myTable').style.display = 'block';
		} else {
			// hide table and change link text to read "show"
			hrefObj.innerText = 'Show table';
			document.getElementById('myTable').style.display = 'none';
		}
	}
//-->
</script>
</head>
<body>
<table id="myTable" border="1"><tr><td>abc</td></tr></table>
<br>
<a href="" onclick="javascript:toggleTable(this); return(false);">Hide table</a>
</body>
</html>

Hope this helps,
Dan
 
I'll try that - Thanks Dan.
 
You can reduce the link code (ever so) slightly by using:
Code:
<a href="" onclick="return toggleTable(this);">Hide table</a>
and then adding a "return false;" statement to the end of the "toggleTable(...)" function.

"onclick" already assumes that it is going to contain JavaScript [thumbsup2]

Pete.


Web Developer &amp; Aptrix / Lotus Workplace Web Content Management (LWWCM) Specialist
w: e: Pete.Raleigh(at)lclimited.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top