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

  • Home
  • SEARCH
  • 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 6033195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:26:54+00:00 2026-05-23T05:26:54+00:00

I’m having trouble getting the syntax write for an $.each equivalent using delegates. All

  • 0

I’m having trouble getting the syntax write for an $.each equivalent using delegates.

All the examples I’ve read just show click or hover but how would I go about writing this with the delegate syntax?

$("ul li a").each(function(){

    $(this).addClass("foo");

});

Cheers!!

Edit: Following the multitude of comments (thanks by the way) I thought I’d better post a more explicit example. My apologies for the lack of detail in my prior question.

Say for example I want to bind tooltips to anchors on a page. I’ll have a lot of items that I want to set up as tips and I want to reduce the memory overhead by using delegation.

As I understand this is how I would get started:

$(document).delegate("a.tooltip", "click", function(event){

// run click event

}).delegate("a.tooltip", "hover", function(event){

// run mousenter, mouseleave events.

});

But lets say for each of those anchors I want to remove the title attribute so it doesn’t show up in the browser when I hover. I would imagine (I am guessing here though) that any native events like that would run before my hover event so I want to remove it before I hover over.

Normally I would use something like this:

$("a.tooltip").each(function () {
    // Store our title attribute and remove it so we don't get browser 
    //tooltips showing up.
    $.prop(this, "data-old-title", $.attr(this, "title"));
}).removeAttr("title");

Can this be done a better way?

From what I understand in the comments and answers delegate covers events only and I will still have to keep anything like this separate.

  • 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-23T05:26:55+00:00Added an answer on May 23, 2026 at 5:26 am

    You probably have a misconception of what .delegate() is or how it works. It’s not some kind of magic function which intercepts all DOM manipulations, it just takes advantage of the event bubbling feature of the DOM, hence it can only be used for events.

    Basically if you have a <div> element, to which you intend to add a <button> later, you can use .delegate() to bind the click event to the <div> itself.

    In the future, when you do add your button, clicking it will fire the click event of the button, and it will carry on bubbling up the DOM, calling each parent element’s click event in succession, until it reaches the document root or one of the handlers call event.stopPropagation(). At one point, it will reach your <div>, where you’ve bound your delegate event. The delegate event will check if the element that originally fired the event matches the original selector (to which you wanted to bind your event in the first place), and if it does, it fires the attached event handler in the context of your button, so within the event handler it will look like the button threw the event.

    As you see there is no magic, it has nothing to do with monitoring the DOM for changes or anything like that. If you want to add special classes as you add elements to the DOM, you have to make sure you call a function yourself whenever that happens.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.