Solved

disable an <a tag with javascript

Posted on 2011-02-16
6
346 Views
Last Modified: 2012-05-11
I am doingn an application and i have a list of items for selection using:
<a href="javascript:additem('itemid')" ID="Xitemid">itemid</a>

in my javascript after processing my item, I am doing:
  document.getElementById("Xitemid").disabled = true;

This lowlights the link, but it is still able to be clicked.

Can I disable the link so that it cannot be clicked without changing the href information? I can do that, but if I enable it, i will need to change the href info back.

thanks
0
Comment
Question by:premiere1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 14

Expert Comment

by:svgmuc
ID: 34908391
Add an onClick="return false;"
0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
ID: 34908522
Continue to disable as you are already, but do this in the A tag:

<a href="javascript:{ if (! document.getElementById('Xitemid').disabled) { additem('itemid') } }" ID="Xitemid">itemid</a>

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
ID: 34908661
What about :
document.getElementById("Xitemid").href = "javascript:void(0);";

Open in new window


Test page :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script language="javascript">
	window.onload = function() {
		disableLink("Xitemid");
	}
	
	function disableLink(id) {
		document.getElementById("Xitemid").href = "javascript:void(0);";
	}
	
	function enableLink(id) {
		document.getElementById(id).href = "javascript:void(" + document.getElementById(id).getAttribute("value") + ");";
	}
	
</script>
</head>

<body>
<a href="javascript:additem('itemid')" ID="Xitemid" value="itemid">itemid</a>
</body>
</html>

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:Proculopsis
ID: 34914132

Try something like this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q__26825862.html</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

jQuery(document).ready( function () {

  $("#toggle").click( toggle );

});

function toggle() {
  var anchor = $("#Xitemid");
  if ( anchor.attr("href") ) {
    anchor.attr( { "rel": anchor.attr("href") } );
    anchor.removeAttr( "href" );
  } else {
    anchor.attr( { "href": anchor.attr("rel") } );
    anchor.removeAttr( "rel" );
  }
}

</script>
</head>
<body>

<a href="javascript:alert('itemid')" ID="Xitemid">itemid</a>

<input id="toggle" type="button" value="Toggle" />

</body>
</html>

Open in new window

0
 

Author Comment

by:premiere1
ID: 34916171
sjklein42, an FYI, your response did not wrap in the box. I had to look at the source to see it.
I liked your solution. I did this:
<a href="javascript:{if (! this.disabled) {move('BOXED','3004230501','500')}}" ID="O3004230501">3004230501</a>

and for the javascript, I did this:

function move(type,order,qty){
 document.getElementById("O"+order).disabled = true;

and it worked well.

I tried:
<a href="javascript:{if (! this.disabled) {move('BOXED',this,'500')}}" ID="O3004230501">3004230501</a>

and this
function move(type,this,qty){
 this.disabled = true;

but it did not work, can you tell me why?

0
 

Author Comment

by:premiere1
ID: 34916419
This should have been:
function move(type,obj,qty){
 obj.disabled = true;

not sure if using "this" would cause a problem.


0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question