Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
$(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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!