Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7822699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:59:50+00:00 2026-06-02T07:59:50+00:00

really basic stuff here. I’d like to give a click function a name and

  • 0

really basic stuff here. I’d like to give a click function a name and assign some parameters to it. The goal is code reusability such that I can write only one generic function for common tasks such as for enabling users to delete various data.

Here’s a jsfiddle to show you what I mean.

And here’s that code:

the HTML:

<button>delete this</button>
<div data-id="3" class="delete">something bad</div>
<div data-id="4" class="delete">something else bad</div>

and the JS:

// this function would be loaded on my site's template and therefore would be available across my entire site.
function deleteThis(data_id){           
   $('button').on('click', 'button', function(){
      $('div[data-id="'+data_id+'"]').hide();  
   });
}  

var clicked_id=3;
function deleteThis(clicked_id); 
// this function would be called on the various pages where users can delete things and this variable, clicked_id, would be assigned=3 by the user's action on that page.

How do I give this button click event a name?

update thanks all! the $('button') should have been $(document.body) or the button’s parent element. It works if you make that simple change. You can also do it as Michael Buen suggests below.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:59:52+00:00Added an answer on June 2, 2026 at 7:59 am

    Just refactor your code, put the delete functionality on its own function

    <button>delete this</button>
    <div data-id="3" class="delete">something bad</div>
    <div data-id="4" class="delete">something else bad</div>
    ​
    
    $('button').on('click', function() { deleteImmediately(3) });
    
    function deleteImmediately(id) { -- refactored code
        $('div[data-id='+id+']').hide();  
    }​
    

    Live test: http://jsfiddle.net/e2kuj/2/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Really basic question here: I've seen a bunch of CoreAudio code that uses memset
Trying to do some really basic math here, but my lack of understanding of
Really basic question I'm sure for some of you Java heads out there. I
This feels like a really basic question, but I can't figure it out anyway..
This I am sure is really basic stuff. I am just beginning using gvim
I could really do with some advice here. I'm new to Django, but understand
I'm trying to do some really simple stuff in C++, but I can't find
Here’s a really really super-basic question. Say I wanted to build – today –
I just switched to Mercurial from SVN. I've done some basic stuff (importing my
I am getting really desperate here. I working with Xcode, trying to implement some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.