views:

85

answers:

4

How do I convert the following javascript to using JQuery?

document.getElementById("asc").removeAttribute("href");
document.getElementById("asc").onclick = "";
document.getElementById("asc").style.textDecoration = "underline"

I think I'm close using the below code but this doesn't quite work.

$('#asc').attr('href', '').click(function() {return false}).css('text-decoration', 'underline');
A: 

The only differences I can see is that the href attribute isn't actually being removed. You're also creating an event handler when the first example doesn't have one.

This will remove the attribute instead:

$('#asc').removeAttr('href').css('text-decoration', 'underline');

If there's already an onclick handler on it, try this:

$('#asc').removeAttr('href').attr('onclick', '').css('text-decoration', 'underline');
Brian McKenna
Close, once I replaced the comma is a semi collon like this --> $('#asc').removeAttr('href', '').css({'text-decoration' : 'underline'}); it work
Peoples
+1  A: 

Why not just

$('#asc').replaceWith($('#asc').text())

which will replace the link with just ordinary text, and save you having to worry about all the aspects of a link.

Peter
A: 

If you can't decide between two of them you want the following...

$('#asc').removeAttr('href').click(function() {return false}).css({'text-decoration' : 'underline'});
Eugene Bulkin
A: 

why assigning a new event handler if you want to get the rid of it

$("#asc")
.attr("href", "")
.unbind("click")
.attr("style", "text-decoration:underline");
Riyono