0

I added a span with a class, but .click does not trigger.

Adding a span class code

var TA = '<span class="TS" id="'+$('#TT').attr('value')+'" style=" color:#fff; margin-left:5px; font-family:arial; font-size:12px">* '+$('#TT').attr('value')+'</span>';
$('#QLT').append(TA);

This adds a span .TS to a div .TX. I want to trigger .TS using .click, the code

  $('.TS').click(function() {
   alert("ok");

  });

But this does not trigger. What is wrong, Appreciate all assistance.

Thanks Jean

2
  • Make sure the handler is bound after the element has been added to the page. Alternatively, you can use live() (api.jquery.com/live) to bind events to elements that aren't in the DOM yet. Commented Jan 22, 2011 at 9:16
  • Could we just concentrate on the problem at hand please...I need a solution to it. - Thanks Commented Jan 22, 2011 at 9:17

2 Answers 2

4

You probably defined the click handler before you did $('#QLT').append(TA). Thus the click handler didn't know about the new element with class .TS when it was bound.

You can manually re-bind it, or use jQuery's cool live function, which will automatically bind when a new element with class TS is inserted into the DOM:

$('.TS').live('click', function() {
   alert("ok");
  });
Sign up to request clarification or add additional context in comments.

Comments

0

It looks like your code is working fine.

Maybe when you're clicking, you're not actually clicking on the text in the span. I think that messes things up sometimes, with click handlers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.