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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:25:41+00:00 2026-05-13T18:25:41+00:00

It seems like there should be a better way to code this: HTML snippet:

  • 0

It seems like there should be a better way to code this:

HTML snippet:

<label onclick="addstuff(this)" id="label_id">Hello</label>

JS/jQuery snippet:

function addstuff(field){
    $('#'+field.id).before('<div>Just a random element being added…</div>');
}

I dislike that I am referencing my field in the Javascript by $(‘#’+field.id) when the field is already being passed in. It seems like there should be another way to reference field in jQuery using the field object that is passed in, which in my mind should be more efficient, but I’m not having any luck figuring it out. Any advice is greatly appreciated.

  • 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-13T18:25:42+00:00Added an answer on May 13, 2026 at 6:25 pm

    This is an easier way.

    $('#label_id').click(addstuff);
    
    function addstuff(){
        $(this).before('<div>Just a random element being added…</div>');
    }
    

    this refers to the label in the event handler function. You can also use the passed in event object in this instance. The event object is implicitly passed to the function as the argument to the first parameter

    function addstuff(event){
        $(event.target).before('<div>Just a random element being added…</div>');
    }
    

    You could also use an anonymous function inline

    $('#label_id').click(function (){
        $(this).before('<div>Just a random element being added…</div>');
    });
    

    but if you want to use the function in more than one location, I would stick to the named function. It also helps when you’re debugging, as you get the name of the function when you step through.

    You must bind the event handler to the element after it has been loaded into the DOM. Do this by either putting your code at the bottom of the page after the markup, or by using

    $(document).ready(function() { /* code here */ });

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

Sidebar

Related Questions

Seems like there should be... Right now it just seems like magic that you
It seems like there should be a removalAllOccuring(Collection) (or similiar) method in Multiset. A
It seems like there used to be way more binary protocols because of the
I been wondering about this for a while. It seems like there are so
I'm noticing code accumulating in my project that looks like this: Response.Redirect(/Foo/Bar.aspx); This seems
This might be a stupid question but if there's a better or proper way
So I'm not sure if there is a better way to do this, but
I am used to the c-style getchar() , but it seems like there is
It seems to me like there's a lot of sheeping going on, with everyone
cgi.escape seems like one possible choice. Does it work well? Is there something that

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.