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 8577229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:06:45+00:00 2026-06-11T20:06:45+00:00

I have a question concerning style/compactness. I have a function which I want to

  • 0

I have a question concerning style/compactness. I have a function which I want to run once when my document loads, and also whenever an event is triggered.

My code looks something like this:

$('.myClass').each(function(){my_function(etc);});
$('.myClass').keyup(function(){my_function(etc);});

I’m wondering if there is a way to combine these two lines of code into one. It works fine at present, but the fact that I’m repeating code has me thinking that there could be a more succinct way to do this.

.bind() and .on() gave me no joy. I am assuming this is because they’re intended for events, and .each() isn’t an event.

Can anybody offer any insight? Cheers

  • 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-06-11T20:06:47+00:00Added an answer on June 11, 2026 at 8:06 pm

    So, http://api.jquery.com/jQuery.each/ is iterator

    The $.each() function is not the same as $(selector).each(), which is used to iterate, exclusively, over a jQuery object. The $.each() function can be used to iterate over any collection, whether it is a map (JavaScript object) or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time. (The value can also be accessed through the this keyword, but Javascript will always wrap the this value as an Object even if it is a simple string or number value.) The method returns its first argument, the object that was iterated.

    keyup is event: http://api.jquery.com/keyup/

    Hope it fits the cause and lemme know if I missed anything! :)

    Try this:

    $(".myClass").each(my_function(etc))
                 .bind("keyup mouseover", my_function(etc));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question concerning this code which I want to run on QNX:
I have a question concerning functions with jQuery. I have a function that once
I have question concerning a function I created. I would like to show the
I have a question concerning subtypes of built-in types and their constructors. I want
I have a question concerning using the toggle function. I have a template that
I have a question concerning Javascript: I want to have the same effect of
I have a question concerning user defined types in DB2(v. 9.7.0.441). I want to
I have a little question concerning html-lists and CSS. I want to have a
I have a question concerning the TextField of Vaadin. I want to block input
I have a question concerning the OOB SharePoint 2010 feature E-mail a Link which

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.