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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:16:05+00:00 2026-06-02T16:16:05+00:00

I would like to trigger a click event when a particular span element is

  • 0

I would like to trigger a click event when a particular span element is clicked on. The span elements are actually buttons which are on top of a table:

The following works:

$("th > span").click(function() {
        alert('hi');
});

The following does not work:

$("th.sorting > span").click(function() {
        alert('hi');
});

The class of the th element changes from sorting to sorting_asc or sorting_desc, and I would like to trigger a particular event depending on which type of button is clicked on.

UPDATE:

Interestingly when I view source I see only the following:

<th><span class = 'btn' style = 'width:90%'>Image<span id='i'></span></span></th>

…but when I ‘inspect element’ through firebug I see:

<th class="sorting" role="columnheader" tabindex="0" aria-controls="dispensers" rowspan="1" colspan="1" style="width: 187px; " aria-label="Image: activate to sort column ascending"><span class="btn" style="width:90%">Image<span id="i"></span></span></th>

I suspect this isn’t the root of my problem though.. as I’m assuming Jquery is aware of the latter element code.

(The jquery plugin ‘datatables’ is responsible for the updated code)

UPDATE:

Final code:

$('th').on('click', '.sorting > span', function() {
        $("span#i").html('');
    jQuery("span#i", this).html(' <i class=\"icon-chevron-up\"></i>');
    });

$('th').on('click', '.sorting_asc > span', function() {
        $("span#i").html('');
    jQuery("span#i", this).html(' <i class=\"icon-chevron-down\"></i>');
    });

$('th').on('click', '.sorting_desc > span', function() {
        $("span#i").html('');
    jQuery("span#i", this).html(' <i class=\"icon-chevron-up\"></i>');
    });

Appearance:

enter image description here

  • 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-02T16:16:06+00:00Added an answer on June 2, 2026 at 4:16 pm
    $('th').on('click', '.sorting > span', function()
    {
        alert('hi');
    });
    

    Thanks to $.on you can bind elements to event handlers making sure if the second part of the selector for the binding changes jQuery will track them for you.

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

Sidebar

Related Questions

How would you trigger a click event from an element that supposedly does not
I would like links to be trigger on double click. Something simple like this:
I would like to trigger an event when my VideoView is done playing? How
I would like to trigger button on document.ready() and do ajax call event on
I have a video view that is full screen and would like to trigger
I would like to have a Maven goal trigger the execution of a java
I would like to fire an UPDATE trigger only if a certain fields has
I would like to create a small Windows Messenger Client that I can trigger
Using the Northwind sample: I would like to create an insert trigger on the
I have this piece of logic I would like to implement as a trigger,

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.