Solved

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

Posted on 2012-04-13
2
495 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
ID: 37841786
check on which element the event is triggered and stop the event propagation (bubbling) when required
0
 

Author Closing Comment

by:ShanghaiD
ID: 37841824
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

DOM Attributes and Properties treatment with jQuery 1.6 by Ivo Stoykov jQuery (http://jquery.com/) 1.6 introduces .prop() (http://api.jquery.com/prop/) and .removeProp() (http://api.jquery.com/removeProp/) methods which allow modifying or removi…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

785 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