Solved

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

Posted on 2012-04-13
2
498 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Hi all! Recently there was EE question and the person wanted to have a multi-column textbox <div> selection, so as a first step to answer I provided a link but that was not complete with JavaScript selection, but had a good style sheet. So as a ques…
There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
The viewer will learn how to dynamically set the form action using jQuery.
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…

713 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