Disable/enable link (anchor tag) with javascript?

2,902 views
Skip to first unread message

Supermoos

unread,
Jul 11, 2008, 6:51:08 PM7/11/08
to MooTools Users
Is there away to disable a anchor tag, so that I can utilize it as a
enable/disable button script?

What I want to do is to enable two buttons (the anchor tags have
backgrounds for normal state and over state set in CSS) upon click on
another button, and by default have the two buttons disabled (no hand
cursor, and no click event).

Supermoos

unread,
Jul 14, 2008, 4:00:50 AM7/14/08
to MooTools Users
Not possible?

Michal

unread,
Jul 14, 2008, 4:36:33 AM7/14/08
to MooTools Users
(Although I suspect bumping is frowned up... so maybe responding to
bumbs is as well...)

I'm not completely sure what you want to do, but do you want to be
able to change an anchor tag from being a regular link, to just being
regular text? (i.e. not show the "I'm a link" mouse cursor over it).
If so, you could just grab the anchor tag, grab its content, and
replace it with a span with the same content? To 'disable' a link,
something like

var nonLink = new Element('span', {'html':$
('myLinkID').getProperty('html'), 'id':'myNonLinkID'}.replaces($
('myLinkID'));

or to "enable" a link:

var link = new Element('a', {'html':$('myLinkID').getProperty('html'),
'id':'myLinkID'}.replaces($('myNonLinkID'));

(This doesn't save the href of the link though, but maybe you don't
need it to)

Michal.

Michal

unread,
Jul 14, 2008, 4:38:36 AM7/14/08
to MooTools Users
> or to "enable" a link:
>
> var link = new Element('a', {'html':$('myLinkID').getProperty('html'),
> 'id':'myLinkID'}.replaces($('myNonLinkID'));

Oops... I mean $('myNonLinkID').getProperty('html') here I think.

Supermoos

unread,
Jul 14, 2008, 9:00:38 AM7/14/08
to MooTools Users
Yeah, I know it's now looked upon nicely, but I just thaught that
maybe someone had needed the same thing before, and therefore might
have a solution :-)

Okay, no easy solution, like button.enable(); - Suppose I'll have to
make it like so myself then, maybe a little class :-P

hellspawn

unread,
Jul 14, 2008, 9:05:21 AM7/14/08
to MooTools Users
a here is the a element as object, get by $, $$ or created...
a.addEvent('click', function(e){ var e = new Event(e).stop(); })

this won't work ?

Bryan Swift

unread,
Jul 14, 2008, 11:23:14 AM7/14/08
to mootool...@googlegroups.com
for those links I would create a functions like enable/disable where disable sets the cursor style to default and attaches a click event like hellspawn suggested, the enable function would set the cursor style to pointer and removes the event added by disable.

keif

unread,
Jul 15, 2008, 1:48:46 AM7/15/08
to MooTools Users
This is the idea I had:
http://bestpract.us/mootools/disable-enable/
<code>
<div class="example">
<span id="enabler">Enable the links</span>
<a href="http://www.google.com">Google</a>
<a href="http://www.yahoo.com">Yahoo!</a>
</div>
<script type="text/javascript" src="/mootools/js/mootools-1.2-
core.js"></script>
<script type="text/javascript" src="/mootools/js/mootools-1.2-
more.js"></script>
<script type="text/javascript">
/*<![CDATA[*/
window.addEvent('domready', function() {
var links = $$('a');
links.addClass('disabled');
links.each(function(link){
var href = link.get('href');
link.erase('href');
link.set('rel', href);
});
alert('links disabled');
$('enabler').set('styles', {'cursor': 'pointer'}).addEvent('click',
function(e){
links.erase('class').addClass('enabled');
links.each(function(link){
var href = link.get('rel');
link.erase('rel');
link.set('href', href);
alert('links enabled');
});
});
});
/*]]>*/
</script>
</code>

On Jul 14, 11:23 am, "Bryan Swift" <bryan.j.sw...@gmail.com> wrote:
> for those links I would create a functions like enable/disable where disable
> sets the cursor style to default and attaches a click event like hellspawn
> suggested, the enable function would set the cursor style to pointer and
> removes the event added by disable.
>

Michal

unread,
Jul 15, 2008, 6:05:47 AM7/15/08
to MooTools Users
Is this the best way to disable a link? It seems slightly un-semantic:
it is still a "link" in the DOM. I'm unsure.

Michal.
Reply all
Reply to author
Forward
0 new messages