Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

108
Vistas
$(this) is not working in my jQuery click function

When click my function, i want to change a parent that have this function element. But $(this) not working.

What is wrong?

function accountConfirm(message, title, yes_label, no_label, callback) {
    console.log($(this));
    $("#accountConfirmModal").attr('data-confirm-yes', false);
    if ($("#accountConfirm").length == 0) {
        .....
    }

    $("#accountConfirmModal .content p").html(message || "");
};

That is the HTML element

<button type="button" id="deleteScrollTop" class="btn btn-delete"
                                        onclick="accountConfirm(jsResources.addressDeleteQuestion,jsResources.addressDeleteTitle, jsResources.yes,jsResources.no ,function(res){
                                            if(res){
                                                window.location.href = '@(Url.RouteUrl("CustomerAddressDelete", new {addressId = address.Id}))'
                                            }
                                        })">
                                    <img src="@Url.ThemeFolderUrl("_images/delete.png")" alt="delete address" />
                                </button>
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You possibly meant to do this

Always a good idea to consolidate on jQuery if you use it and not use inline event handlers

Assuming the button ID is unique

function accountConfirm(message, title, yes_label, no_label, callback) {
  $("#accountConfirmModal").attr('data-confirm-yes', false);
  if ($("#accountConfirm").length == 0) {
    .....
  }

  $("#accountConfirmModal .content p").html(message || "");
};

const route = function(res) {
  if (res) {
    window.location.href = '@(Url.RouteUrl("CustomerAddressDelete", new {addressId = address.Id}))'
  }
}
$("#deleteScrollTop").on("click", function() {
  console.log($(this));
  accountConfirm(jsResources.addressDeleteQuestion,
    jsResources.addressDeleteTitle,
    jsResources.yes,
    jsResources.no, route)
})
<button type="button" id="deleteScrollTop" class="btn btn-delete">
  <img src="@Url.ThemeFolderUrl("_images/delete.png")" alt="delete address" />
</button>
about 4 years ago · Juan Pablo Isaza Denunciar

0

When you invoke a function in a onclick attribute, it is actually invoked by the window object, so this will become the window object itself, not the button.

If you want the this become the button itself, you will have to use apply method of the function.

For example:

function somefunc(arg1, arg2){
    console.log("This is ", this);
    console.log("Arg1: ", arg1);
    console.log("Arg2: ", arg2);
}

In onclick

<button onclick="somefunc.apply(this, [1, 2])">Button</button>
about 4 years ago · Juan Pablo Isaza Denunciar

0

If you want to access "this" in an onClick-event, you do it like this:

<button onclick="accountConfirm(this)">
  your button
</button>

<script>
  function accountConfirm(elmnt) {
    console.log("this: ", elmnt);
   
  };
</script>
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda