Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6545297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:34:08+00:00 2026-05-25T11:34:08+00:00

If you bind a click event to a class: $(.testClass).click( function() { $( this

  • 0

If you bind a click event to a class:

$(".testClass").click(  function() {
    $( this ).css( 'top' : '10px' );    
});

In this example, do all elements with that class get assigned the attribute value? Is there a way to apply the attribute only to the class member that was clicked on?

This is what appears to be happening with this makeDraggable plugin I grabbed off the web (and i have modified slightly) – note that e is set = to this. When I apply that to a class selector, all members of the class move, even though I only drag one. Why is that?:

jQuery.fn.makeDraggable = function(delay) {
    if (typeof(delay) == 'undefined') delay = 200;
    var a = false, // drag active
    d = jQuery(document), // document jQuery
    e = jQuery(this), // current element
    c = e.css('cursor'), // current cursor
    p = [null,null], // clicked point
    m = null, // current mouseDown event
    t = null, // timeout id
    bl = function(e) { // blocks an event (to prevent selecting text)
            var tN = e.target.tagName;
            if( ! ( tN == "INPUT" || tN == "LABEL" || tN == "TEXTAREA" )){
               try { // will fail with IE & timeout
                  e.stopPropagation();
                  e.preventDefault();
                    } catch (e) {}; // but will still remain usable
            }
    },
    md = function(e_md) { // mousedown
            bl(e_md); // text selection would occur if not blocked here
            if (a) { // drag active
                    e.css('opacity', '0.8')
                    .css('cursor', 'move')
                    .css('outline', '2px solid #fc0')
                    .css('z-index', '20');
                    d.bind('mousemove', mm);
                    p = [e_md.clientX - e.position().left, e_md.clientY - e.position().top];
                    e.trigger('dragstart');
            } else { // just wait
                    var tN = e.target.tagName;
                    if( ! ( tN == "INPUT" || tN == "LABEL" || tN == "TEXTAREA" )){
                            m = e_md;
                            t = setTimeout(to, delay);
                    }
            }
    },
    mu = function(e_mu) { // mouseup
            if (a) { // drag active
                    bl(e_mu); // text selection would occur if not blocked here
                    d.unbind('mousemove', mm);
                    e.css('opacity', '1')
                    .css('cursor', c)
                    .css('outline', 'none')
                    .trigger('dragend');
            } 
            a = false;
            clearTimeout(t);
    },
    mm = function(e_mm) { // mousemove
            bl(e_mm); // text selection would occur if not blocked here
            e.css('left', (e_mm.clientX - p[0]) + 'px')
            .css('top', (e_mm.clientY - p[1]) + 'px');
    },
    to = function() { // timeout
            a = true;
            if (m) md(m);
    };
    e.bind('mousedown', md).bind('mouseup', mu);
    return e;
};

here’s the initialization code:

 jQuery('.testClass').makeDraggable();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:34:09+00:00Added an answer on May 25, 2026 at 11:34 am

    This code:

    $(".testClass").click(  function() {
        $( this ).css( 'top' : '10px' );    
    });
    

    Says that when a member of the testClass class is clicked, then its top CSS property should be set to 10px.

    That plugin should behave the same way. Could you post the code you use to initialize the plugin and possibly the other code in your page?


    Fixed it.

    The problem was here:

    md = function(e_md) { // mousedown
    bl(e_md); // text selection would occur if not blocked here
    
    if (a) { // drag active
        e.css('opacity', '0.8').css('cursor', 'move').css('outline', '2px solid #fc0').css('z-index', '20');
        d.bind('mousemove', mm);
    

    In this context, e is defined to be the selector that is used to initialize, which happens to be all of the elements in that class. I just redefined e to be $(e_md.target), the target of the event.

    A demo and the new code is here: http://jsfiddle.net/H2ahz/7/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to be able to do this $('.class').bind({'click':'function(){ ... }'}); // note that
Have same js code: $(#pr_list .c).bind({ click: function(event) { var m = $(this); var
OK, so, I use livequery() to bind a function to the click event of
I have multiple class events bind together for a click event , i want
I am trying to attach 'click' events to all elements of a particular class.
Using jQuery, how do you bind a click event to a table cell (below,
$(#dvMyDIV).bind(resize, function(){ alert(Resized); }); or $(#dvMyDIV).resize(function(){ alert(Resized); }); The questions Why is this not
I am trying to bind a click event to a button within a usercontrol
it is possible to bind Live('hover') and bind('contextmenu') on the same .class? This code
How do I bind click event on dynamic generated buttons in backbone.js? window.PackageView =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.