Solved

jquery mousedown to run once on selected element but remain for use by other elements

Posted on 2012-04-13
2
492 Views
Last Modified: 2012-08-13
I have a '#tsTable tbody' to which I programatically attach tr and td elements, with one column of the td elements each having the same class ('name').

I have a function which watches for a mousedown event on any element with class ('name'), as follows:

$('#tsTable tbody').on('mousedown', '.name, function(event) {
  var currentName = $(this).text();
  $(this).attr('name', currentName);//store name for later
});

Open in new window

I need to stop this mousedown event being triggered more than once for the same element to protect against accidental multiple clicks by the user.  I tried to do this by using unbind() as follows:

$('#tsTable tbody').on('mousedown', '.name, function(event) {
  var currentName = $(this).text();
  $(this).attr('name', currentName);//store name for later
  $(this).unbind(event);
});

Open in new window

However, this results in the function being unloaded not only for the first element which is clicked, but also for every other element of the same class ('name'), which is not what I want.

As further explanation, I want to do this:
1. run the mousedown function once only on the clicked element (and ignore further clicks of that same element);
2. then do some other processing with the captured 'currentName' (and other things);
3. then after that processing, reactivate the first mousedown function so it can be reused on the page again (on a different clicked element).
0
Comment
Question by:ShanghaiD
2 Comments
 
LVL 8

Accepted Solution

by:
X-treem earned 500 total points
Comment Utility
check on which element the event is triggered and stop the event propagation (bubbling) when required
0
 

Author Closing Comment

by:ShanghaiD
Comment Utility
Yes, I'll accept this as the solution.  In fact I can do this by first testing for the existence of the attribute value which is stored on the first mousedown (only) as follows:

$('#tsTable tbody').on('mousedown', '.name, function(event) {
  if($(this).attr('name') == undefined) {
    var currentName = $(this).text();
    $(this).attr('name', currentName);//store name for later
  }
});
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

I have answered several questions lately that were solved utilizing the power of jQuery's AJAX functions, so I thought I would write an article demonstrating the ease of use. Why should I use jQuery as opposed to regular JavaScript? Now I know…
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now