I have a form with multiple text boxes and a set of buttons. Using the Javascript below, I am able to click a button and insert text into one of the named boxes.
Is it possible to insert text into the most recent/active textbox when a button is clicked?
Currently I have this, but it's using the specific ID of a text box, rather than the most recently used/active one;
$( 'input[type=button]' ).on('click', function(){
var cursorPos = $('#my_text_box').prop('selectionStart');
var v = $('#my_text_box').val();
var textBefore = v.substring(0, cursorPos );
var textAfter = v.substring( cursorPos, v.length );
$('#my_text_box').val( textBefore+ $(this).val() +textAfter );
});
Example on JSFiddle for more clarity;
http://jsfiddle.net/bd1xf0sj/1/
The idea being that depending upon which text box you're in when the button is clicked, instead of using the ID of a certain textbox. The idea being that the buttons can be clicked and any textbox can be used.
from How can I insert text via button click at the cursor in the most recent textbox used?
No comments:
Post a Comment