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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Find the Index of Specific Form Link in JS?

Status
Not open for further replies.

soho34

IS-IT--Management
Dec 28, 2004
102
US
Hi,

I have a link defined as follows:

<a href="" id="clrest">Restrict</a>

Now, in my JS function, I'm disabling/enabling the link as follows:

document.links[2].disabled = true;

However, I need to change my JS statement from referencing "2" to referencing whatever position that link happens to be, so I modified my code to this:

function disableLink(){

var rnum = document.getElementById("clrest").value;
document.links[rnum].disabled = true;

}

But for rnum, I keep getting "undefined". What is my problem? I thought using the getElementByID and the value attribute would return the position of that particular link.

Am I doing something wrong?

Thanks in advance for your help.

soho34
 
Since the question ought to be in this forum anyway, I use this thread instead on the CSS forum one.

document.getElementById("clrest").innerHTML

Will only get you the word "Restrict", which is not the index of the link. However, I don't see why you need the index.
Code:
function disableLink(){
    document.getElementById("clrest").disabled = true;
}

Check out my blog: defrex.com
 
OK...

How about this way to "disable" links:
Code:
<html>
<head><title>Test</title>
<script type="text/javascript">
// disable a link
function disableLink(data) {
	var theLinkToDisable = document.getElementById(data);
	if (theLinkToDisable) {
		if (theLinkToDisable.tagName.toLowerCase() == 'a') {
			theLinkToDisable.onclick = function() {return false;};
			theLinkToDisable.className = 'disabledLink';
		}
	}
}
</script>
<style type="text/css">
	a.control {
		text-decoration:  none;
		color: red;
		font-weight: bold;
		padding-left: 1em;
	}
	a.disabledLink {
		text-decoration: line-through;
		font-style: italic;
	}
</style>
</head>
<body>
	<ul>
		<li>
			<a href="[URL unfurl="true"]http://www.tek-tips.com"[/URL] id="cltektips">[URL unfurl="true"]http://www.tek-tips.com</a>[/URL]
			<a href="#" onclick="disableLink('cltektips'); return false;" class="control">disable</a>
		</li>
		<li>
			<a href="[URL unfurl="true"]http://www.aol.com"[/URL] id="claol">[URL unfurl="true"]http://www.aol.com</a>[/URL]
			<a href="#" onclick="disableLink('claol'); return false;" class="control">disable</a>
		</li>
		<li>
			<a href="[URL unfurl="true"]http://www.google.co.uk"[/URL] id="clgoogleuk">[URL unfurl="true"]http://www.google.co.uk</a>[/URL]
			<a href="#" onclick="disableLink('clgoogleuk'); return false;" class="control">disable</a>
		</li>
		<li>
			<a href="[URL unfurl="true"]http://www.google.com"[/URL] id="clgooglecom">[URL unfurl="true"]http://www.google.com</a>[/URL]
			<a href="#" onclick="disableLink('clgooglecom'); return false;" class="control">disable</a>
		</li>
	</ul>
</body>
</html>
There is plenty of room for improvement in that code... I was just showing the idea.

Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
If really the link index is in question, it is how you get it like this. Whether .disabled is effective or not is up to op's judgement. Call the function by disableLink("clrest") for instance.
[tt]
function disableLink(sid) {
var olink=document.getElementById(sid);
var clink=document.links;
for (var i=0;i<clink.length;i++) {
if (clink===olink) {
alert( "my link index = " + i);
//olink.disabled=true; //if op likes, is it effective?!
return i; //link index
}
}
alert("I am no link.");
return -1; //not a link
}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top