1

How do I make this trigger when I select the text in the input?

It has to be live/delegate

$('input:text').live('select', function () {
    alert("selected");
});

Updated to input:text by popular demand. I still do not get an alert message when selecting text in input boxes. The example on http://api.jquery.com/select/ gives me the result I want. I just need it to work on a .live()

4
  • 2
    You're aware that :input should be input? The first is a pseudo element, the second is a real element. May 31, 2011 at 20:56
  • @David Thomas: See api.jquery.com/input-selector It's valid but not recommended to be used like this. May 31, 2011 at 21:00
  • @Drackir, you're right; hence my comment to you below @colinmarc's answer. :) May 31, 2011 at 21:03
  • @colinmarc what dosen't happen is an alert box saying "selected" when I highlight text in an input box.
    – colemande
    May 31, 2011 at 21:39

2 Answers 2

3

You're selecting all inputs wrong. You should be using $('input'), if that's what you want to do.

Or even better, since only text inputs can have text selected in them, try $('input:text')

I think the issue lies in this quote from the .live() documentation:

DOM traversal methods are not supported for finding elements to send to .live().

When you use only a pseudo-class, jQuery adds the everything selector (*), like $('*:input'), which might fail to delegate based on the method it uses to fill the result array. Just a shot in the dark, but the best I can do without knowing more about what's going wrong.

2
  • @Drackir, you're right (and +1), but for this usage it would be a little better to use $('input:text, textarea'), admittedly with the subjective disclaimer of 'in my opinion.' May 31, 2011 at 21:00
  • @Drackir you're right, but I still think this might be the issue. See my edit.
    – colinmarc
    May 31, 2011 at 21:12
2

Your code works fine for me here(jsFiddle) in Opera, FF, Safari and Chrome. IE8 doesn't seem to like it, however.

I thought maybe it was because .live() binds an event handler to the document and it's possible IE doesn't have an onselect event on the document so I tried limiting the scope of it to the body and even to a wrapper element with no luck.

This could be a bug with jQuery.

4
  • And there lies my problem... I have to use IE.
    – colemande
    May 31, 2011 at 21:35
  • 1
    @colemande: You could always add the event using .select() when the elements are created. Though, if you have MANY of them, this could cause issues since it means each one has an event handler (vs using .live which only has one). May 31, 2011 at 21:48
  • SOLUTION: $('input:text').bind('select', function () { textSelected = true; });
    – colemande
    May 31, 2011 at 22:13
  • 1
    @colemande: That's the same as using .select(). It will apply to elements already created but not to new elements added to the dom after the bind call. See here (try highlighting the text in the last box added by JavaScript) May 31, 2011 at 22:17

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.