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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:38:46+00:00 2026-05-27T19:38:46+00:00

How can I prepend to a class specifically descended from $(this) e.g. I only

  • 0

How can I prepend to a class specifically descended from $(this) e.g. I only want to prepend to the div that’s actually clicked in this example…

<div>
 <div class='target'></div>
</div>

<div>
 <div class='target'></div>
</div>



$('div').click(function(){
  $('<div>hello</div>').prependTo('.target');
});
  • 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-27T19:38:47+00:00Added an answer on May 27, 2026 at 7:38 pm

    Only hook the relevant divs, then use this within the handler (but see also below, I can read your question two different ways):

    $('div.target').click(function(){
      $('<div>hello</div>').prependTo(this);
    });
    

    Within an event handler hooked up via jQuery, this refers to the specific DOM element you hooked the handler to that received the event. See the bind or on documentation for details.

    Live example (You also had the } and ) at the end reversed, causing a syntax error.)

    If your goal is to have a click anywhere on the outer div prepend in front of the target div, it’s more like this:

    $('div').click(function(){
      // Look for "target" divs within this div
      var target = $(this).find("div.target");
    
      // Did we find any?
      if (target[0]) {
        // Yes, prepend to it/them
        $('<div>hello</div>').prependTo(target);
    
        // And stop the event
        return false;
      }
    });
    

    Live example There we’re using this again, but this time we’re using it to find out if the div the event has propagated to contains a div with the class “target”. If so, we prepend your “hello” div and stop the event via return false; (you could use event.stopPropagation(); instead).

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

Sidebar

Related Questions

How can I call prepend on a div 's id and class ? I
Can a LINQ enabled app run on a machine that only has the .NET
This should be simple, but I can't figure it out For example, lets assume
I want to represent a 2D shape in such a way that it can
var str = $('.rating').html(); $.trim(str); $('.rating').prepend.attr('class', 'rating-' + str.replace(/\s/g, )); <div class=rating>1</div> <div class=rating>2</div>
I have a caching class that has a flat file driver, meaning you can
I want to clone and prepend a div, however I want to change the
I am using prettyCheckboxes and I want to change it so that the div
I can add an element to a form like this: $form->addElement($element); However, that will
I'm looking at this code, I can see that is says actions: show and

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.