Bablu Ahmed
Bablu Ahmed

Reputation: 5010

How to find a specific id by clicking on a button anywhere on a page in jQuery

I am trying to get a specific id by clicking on a button that can be anywhere on the page.

I am trying as follows:

$(document).on('click','.cia_delete',function(e){
    e.preventDefault();
    e.stopPropagation();
    let thisBtn = $(this);
    let refreshArea = thisBtn.closest("body").find("#cia_refresh_area");
});

In the above code, I want to find #cia_refresh_area that can be anywhere in the document.

Is the above code correct?

Upvotes: 0

Views: 64

Answers (1)

Shidersz
Shidersz

Reputation: 17190

Based on the feedbacks of your question, your code should be like this:

$(".cia_delete").click(function(e)
{
    e.preventDefault();
    e.stopPropagation();

    let refreshArea = $("#cia_refresh_area");
});

Anyways, I suggest you to read more about the JQuery selectors. Like a basic example related to the code:

  • $(".cia_delete") will match against (and will selects) all elements that have the class .cia_delete
  • $("#cia_refresh_area") will match against (and will selects) the element with ID attribute equal to cia_refresh_area

Upvotes: 2

Related Questions