0

I'm giving a value to a textbox and then giving focus to it.

document.getElementById('textbox').value="abcd";
document.getElementById('textbox').focus();

Then, I am creating a keyboard event and trying to fire the CTRL+A key combination (trying to select the text inside the textbox) by writing the following code:

var myEvent = document.createEvent('KeyboardEvent');

myEvent.initKeyEvent("keypress", true, true, window, true, false, false, false, 0, 97);

document.getElementById('textbox').dispatchEvent(myEvent);

But the above code doesn't select the text inside the textbox.

How to select the text creating the keyboard event ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sanjeev
  • 855
  • 1
  • 9
  • 15
  • Similiar http://stackoverflow.com/questions/4067469/selecting-all-text-in-html-text-input-when-clicked – Musa Jan 25 '14 at 17:55

1 Answers1

3

You can't trigger browser keypress behavior with JavaScript simulated keypresses. You can only trigger your own function. What that means if that if you add a keypress event listener that checks if the a key is pressed and then does something, you can trigger that behavior, but you can't for example make the browser pull up it's "find" bar when you trigger ctrl+F. That would be a security issue.

The appropriate way would be to write your own function for selecting and fire it whenever you need it.

This should do what you're looking for: Live demo (click).

<input type="text" id="my-input" value="Some text.">

JavaScript:

var myInput = document.getElementById('my-input');

myInput.addEventListener('click', function() {
  this.focus();
  this.select();
});

var event = new Event('click');
myInput.dispatchEvent(event);
m59
  • 43,214
  • 14
  • 119
  • 136
  • I understand that this is a security issue but still, is there any way to achieve this functionality ? – Sanjeev Jan 25 '14 at 17:59
  • @user2856930 see the demo I posted. – m59 Jan 25 '14 at 17:59
  • The example that you have posted to select the text is fine, but I need to achieve other things too with the key combinations. – Sanjeev Jan 25 '14 at 18:02
  • @user2856930 such as? You have to ask the question you want answered...how can I know what else you need unless you tell me? – m59 Jan 25 '14 at 18:04
  • Such as "Opening a new Tab", "Closing a tab". – Sanjeev Jan 25 '14 at 18:06
  • @user2856930 again, that has nothing to do with keypresses. That's just `window.open()` and `window.close()`. – m59 Jan 25 '14 at 18:08
  • Mozilla doesn't allow window.close(). – Sanjeev Jan 25 '14 at 18:09
  • @user2856930 View this demo in Firefox - window.close() works fine. http://plnkr.co/edit/iovbGvita1BNiXOQ029H?p=preview – m59 Jan 25 '14 at 18:22
  • Check this... it will not work if u see this in firefox. http://jsfiddle.net/EvXsk/2/ – Sanjeev Jan 25 '14 at 18:38
  • @user2856930 of course not! It sounds like you're up to no good. You can only close a window that you opened. Everything you're trying to do would only be useful to someone trying to harm/attack the user. http://jsfiddle.net/EvXsk/3/ – m59 Jan 25 '14 at 18:46
  • Actually when I open a popup in IE, the parent window automatically closes, I want to achieve the same... – Sanjeev Jan 25 '14 at 18:54