Solved

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

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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

623 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