I’m testing a syntax for an email input. And it is alerting some message on the web browser if the email syntax is wrong. After the user clicks ok on that messsage I want the browser to select the text.
var input = document.createElement('input');
document.body.appendChild(input);
input.onblur = function(){
var reg = /^([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
if(reg.test(this.value) == false){
alert('Wrong syntax on e-mail');
$(this).select();//This doesn't work!
}
}
Interesting issue, it looks like a security-feature. Keep in mind that your script blocks the UI as long as you didn’t type a accepted email-adress.
This works for me:
As you see it uses a timeout, but I don’t think that the delayed call is the key, the important thing is the different scope where the function will be executed now(scope is now window instead of the input).