Remove an attribute from the <a> element when there is a specific attribute - jQuery

Asked

Viewed 83 times

1

In the HTML of the page I’m adjusting, it contains several links with a function in the onClick:

<a href='#' onClick='funcao(100)'>Link</a>

and the current Javascript is like this:

<script>
function funcao(numero) {
  $.ajax({
        type: 'POST',
        url: "url-checka-numero/",
        data: { numero: numero },
        success: function(data) {
            $('.return').html(data);
        }
    });
}
</script>

I tried to disable the onClick of the element <a> by clicking on it:

<script>
function funcao(numero) {
$(this).removeAttr('onClick');
  $.ajax({
        type: 'POST',
        url: "url-checka-numero/",
        data: { numero: numero },
        success: function(data) {
            $('.return').html(data);
        }
    });
}
</script>

The intention is to exclude only the onClick of the elements <a> containing the onclick='funcao(n)', and not the others, but what I did removes everything.

2 answers

2

You can pass the element that was clicked as parameter to its function, try so:

<script>
function funcao(numero, el) {
    $(el).removeAttr('onClick');
    $.ajax({
        type: 'POST',
        url: "url-checka-numero/",
        data: { numero: numero },
        success: function (data) {
            $('.return').html(data);
        }
    });
}
</script>

And in HTML, upgrade to:

<a href='#' onClick='funcao(100, this)'>Link</a>

Follow an example of what you need working: https://codepen.io/tkrempser/pen/GawNaZ

  • Thank you for your wisdom, I will test too, another way of doing will help for other adjustments I’ve seen.

0


You can use the function attr, as follows: documentation

The removeAttr, has some problems to run on some browsers, to avoid light complications as usual use the function attr or prop for those functions.

$(this).attr("onclick", null);
  • @Henriqueepedro, good night, the onclick will be null, and then in the answer 'Success' of $.ajax has to recover what was before doing so?

  • 1

    Yes, create a variable to store this value, put this variable at the beginning of the function, var onclick = $(this). attr("onclick")

  • But it would not solve, because each onclick='Function(n)' has a different value, and should cancel all, so that there is no double click or other click on other elements <a> otherwise it will generate a double purchase, has user who does not wait for the server answer and is clicking rs, but if it’s not all right, I’ll keep it that way until I find a way around it, obg.

  • 1

    I understood, in this case I would have to use each one on all the links, that I would have to use an id on each link and save in an array the id of the link and the onlick attribute, or as I use that I think is simpler because I have the disabled attribute, would use button and start running a disabled assign function for all Buttons and at the end of the remove function.

  • Hi, solved the way you said, thank you!

Browser other questions tagged

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