How to remove a "href" from a <a> tag with Jquery/Javascript?

Asked

Viewed 8,117 times

12

I have a tag <a> with hyperlink properly set. However, I would like to remove it when firing from an event. The same tag has a properly set ID, too (unique, valid, and related).

I wonder if there is a function to remove the attribute href and thus make the link invalid, or if there is a way to just disable that link.

6 answers

15


The modern solution to disable a link is CSS only: (for modern browsers)

$('#minhaID').css("pointer-events", "none");

// e para re-activar basta usar:
$('#minhaID').css("pointer-events", "");

Demo

You can use it too:

$('#minhaID').attr("href", "");

Or else:

$('#minhaID').removeAttr('href'); // note que esta opcção remove algumas caracteristicas importantes do elemento como focus.

depending on the use you want to give.

Note: Chrome has an open discussion about the ability to stop supporting Pointer-Events.

  • 1

    Interesting solution, too. Congratulations!

  • can you tell me if it’s possible to reverse this process and if so, how?

  • 1

    @Dennisbraga, yes, it’s easy, just use $('#minhaID').css("pointer-events", ""); - Example: http://jsfiddle.net/Tn52U/1/

  • 1

    Edit your answer and add that. It’s really cool that you can remove the link without losing information and power, simply via CSS, reactivate it. : D

  • 1

    @Dennisbraga, done! good idea.

  • This css solution is supported by older browsers?

  • @No. I added a link to give this information.

  • Just be careful when using Pointer-Events, as it only works on 3 browsers... as per the specification: http://caniuse.com/#search=Pointer%20events

Show 3 more comments

11

Would just be that?

$('a').removeAttr('href');
  • Theoretically, yes... but it didn’t bother me! I thought I was doing it wrong, or something like that. I’m calling for the link ID, but I don’t think that’s a point.

  • UPDATE: It worked. That’s exactly what it was, but another part of my code was getting in the way of it working properly. I feel kind of covered up asking you that right now!'

  • 1

    It’s part, it happens ^^

7

There are several ways, by pure javascript the ideal is that you use the reference of your element and in it use the method removeAttribute, examples:

document.querySelector('#minhaAncora').removeAttribute('href');
document.getElementById('minhaAncora').removeAttribute('href');

But when you remove this property from the anchor, this anchor ceases to function as anchor, losing the characteristics of the pseudo selectors :hover, :active, :focus and :visited what can be an unwanted behavior.

In this case you can replace the attribute value with a hash, example:

document.querySelector('#minhaAncora').setAttribute('href', '#');

However this can influence the browser scroll depending on the elements and page size, so another solution is to put in place a script that does nothing, example:

document.querySelector('#minhaAncora').setAttribute('href', 'javascript: ');

It will maintain anchor properties but will do nothing when clicked. Because the browser interprets this as an "evaluation" of code during the click event, but since there is no code, it ignores the command.

Tip for those who use jQuery would look like this:

$('#minhaAncora').attr('href', 'javascript: '); // modifica pra não fazer nada
$('#minhaAncora').attr('href', '#'); // faz o scroll mover-se para inicio do documento, mas não muda de página
$('#minhaAncora').removeAttr('href'); // remove o atributo da tag

If you do not want to remove, you can use the pointer-event CSS to invalidate the use of cursor events in the tag, but this does not remove the link that can still be accessible with Focus (use tab) then enter, only modern browsers have support.

If you don’t want to remove the attribute, but just remove their event, you can prevent the default action with the following code:

document.querySelector('#minhaAncora').addEventListener('click', function (e) {
    e.preventDefault();
});
// Compatível com todos browsers modernos, IE antigos precisa retornar false
// e a forma de adicionar eventos é por outro método.
// No caso use o jQuery:
$('#minhaAncora').on('click', function (e) {
    e.preventDefault();
});
  • 1

    Very solid answer, too. Good job.

6

I suggest, in addition to removing the attribute, adding a style to be visible to the user:

$('#link').removeAttr('href').css({
    'cursor': 'not-allowed',
    'color': '#AAAAAA',
    'text-decoration': 'line-through'
});

Example in jsfiddle.

5

Using pure javascript:

document.getElementById('id').removeAttribute('href');

4

If this is your case, you can also cancel the default anchor redirect event with the event itself:

$('a[href]').on('click', function(event) {

    event.preventDefault();

});
  • 2

    Why not edit your reply and add that content instead of creating another answer?

  • This way when the anchor is clicked (if it is your case) it will not redirect to the reference.

  • This function is much more interesting, so I do not lose the information from my link.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.