Solved

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

Posted on 2012-04-13
2
500 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

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…
The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
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…

734 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