0

I'm trying to dynamically set some text in an input field, and then display it, and finally auto-highlight/auto-select it.

If I don't use .val() and instead use value="something" hardcoded on the input it works perfectly.

If I add .val() to the method chain, it just puts the cursor in the box but doesn't highlight the text.

Any idea how I might do this?

In the example below, $(this) refers to the link they click on "Rename" which causes inline edit box to appear. The .next() refers to the input.

$(this).hide().next().val(oldName).show().focus();    
Tallboy
  • 12,847
  • 13
  • 82
  • 173

1 Answers1

1
$(this).hide().next().val(oldName).show().focus().select();

if that's the moment you want it to happen. It will apply to the .next() element, not $(this) but since you've hidden $(this) it's probably what you're after.

Popnoodles
  • 28,090
  • 2
  • 45
  • 53