Skip to content

Commit 677302c

Browse files
committed
Preserve selectionStart and selectionEnd on focused input when replacing form
1 parent 4c40c3a commit 677302c

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

widget-customizer.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,8 +1020,14 @@ var WidgetCustomizer = ( function ($) {
10201020
var widget_content = control.container.find( '.widget-content' );
10211021

10221022
var element_id_to_refocus = null;
1023+
var active_input_selection_start = null;
1024+
var active_input_selection_end = null;
1025+
// @todo Support more selectors than IDs?
10231026
if ( $.contains( control.container[0], document.activeElement ) && $( document.activeElement ).is( '[id]' ) ) {
10241027
element_id_to_refocus = $( document.activeElement ).prop( 'id' );
1028+
// @todo IE8 support: http://stackoverflow.com/a/4207763/93579
1029+
active_input_selection_start = $( document.activeElement ).prop( 'selectionStart' );
1030+
active_input_selection_end = $( document.activeElement ).prop( 'selectionEnd' );
10251031
}
10261032

10271033
control.container.addClass( 'widget-form-loading' );
@@ -1092,10 +1098,13 @@ var WidgetCustomizer = ( function ($) {
10921098
widget_content.html( r.data.form );
10931099
// @todo yellowfade all widget-inside?
10941100
if ( element_id_to_refocus ) {
1095-
// @todo Prevent focus() from doing a select()
1096-
// @todo try to preserve cursor location after replacing value
10971101
// not using jQuery selector so we don't have to worry about escaping IDs with brackets and other characters
1098-
$( document.getElementById( element_id_to_refocus ) ).focus();
1102+
$( document.getElementById( element_id_to_refocus ) )
1103+
.prop( {
1104+
selectionStart: active_input_selection_start,
1105+
selectionEnd: active_input_selection_end
1106+
} )
1107+
.focus();
10991108
}
11001109
}
11011110

0 commit comments

Comments
 (0)