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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:52:21+00:00 2026-06-10T10:52:21+00:00

I dynamically create a new div (with a textbox class and ID), and some

  • 0

I dynamically create a new div (with a “textbox” class and ID), and some other elements inside of it, and later on in my code I bind that div to the click event, and display the element clicked, like so:

$('#textbox_'+i).bind('click', function(event){
    alert(event.target.className);
}

This is fine, and it’ll give me textbox as one of the classes displayed.
But event.target.hasClass() doesn’t seem to work. So, when I do the following, nothing happens:

$('#textbox_'+i).bind('click', function(event){
    if(event.target.hasClass('textbox')) { alert('got it!'); }
}

I tried it a few different ways and it appears to me that event.target.hasClass() just doesn’t work. Is there another way to deal with events or am I doing something wrong?

  • 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-10T10:52:23+00:00Added an answer on June 10, 2026 at 10:52 am

    You’re trying to use a jQuery method, hasClass(), on a standard DOM element. You need to convert the plain JS DOM element e.target to a jQuery object, like so:

    $(event.target).hasClass('textbox')
    

    And you end up with :

    $('#textbox_'+i).on('click', function(event){
         if( $(event.target).hasClass('textbox')) alert('got it!');
    });
    

    Notice the use of on(), the proper closing of the click function, and you don’t need curly brackets in your if statement if you’re only executing a simple alert.

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

Sidebar

Related Questions

I have some code that dynamically creates a new button through JavaScript that when
I have this JS code that I use to create a new div (I
I have a container div inside of which i want to dynamically create other
I am trying to reuse shape data to dynamically create new shapes in code.
How would you create a new html li element dynamically in the code behind
I'm creating DOM elements dynamically (more specifically, using jQuery to create a DIV containing
I have a page that contains some dynamically created controls (TextBox and DropDownList). When
1 .I want to dynamically generate div containing textbox with unique id on click
Is this possible to create new {variable = x.something} and specify variable name dynamically?
Is it possible to create a new List<T> where the T is dynamically set

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.