简体   繁体   中英

Focus on a textarea after custom text on a bootstrap floating modal form

I am using bootstrap to invoke a simple modal form which contains only a textarea field and couple of buttons. I am setting some custom text (variable length) in the textarea while invoking the modal form. What I want is to focus on the textarea field after the custom text so the user can start typing after that. I have mentioned my implemenation of this below. Can anyone tell how to achieve this?

Here is the Fiddle: http://jsfiddle.net/jLymtdg8/

A bit explanation here as well -

Here is my modal (all bootstrap stuff)-

<div class="modal fade" id="msg" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
            <h4 class="modal-title" id="myModalLabel">Type a message</h4>
        </div>
        <div class="modal-body">
            <textarea id="Message" class="form-control"></textarea>
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default btn-sm" data-dismiss="modal">Cancel</button>
            <button id="PostModalButton" class="btn btn-primary btn-sm">Post</button>
        </div>
    </div>
</div>

And here is how it gets invoked -

<div class="pull-right">
        <button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#msg" data-screenname="Type after this">
            Post A Message
        </button>
</div>

This is how I am prefilling the textarea and setting focus, but this is only setting cursor on first line rather than after screen_name -

$(this).on('shown.bs.modal', '#msg', setFieldAndFocus)

var setFieldAndFocus = function () {

    var screen_name = $("button[data-target='#msg']").data("screenname");
    $("#Message").val(screen_name + " ");
    $("#Message").focus();
};

You can take reference of this link : http://jsfiddle.net/3kgbG/433/ It is working for me.

 $('.launchConfirm').on('click', function (e) { $('#confirm') .modal({ backdrop: 'static', keyboard: false }) .on('') .one('click', '[data-value]', function (e) { if($(this).data('value')) { //alert('confirmed'); } else { //alert('canceled'); } }); }); $('#confirm').on('shown', function () { $('#txtDemo').val($('#txtDemo').val()); $('#txtDemo').focus(); // do something… }) 
 body, .modal-open .page-container, .modal-open .page-container .navbar-fixed-top, .modal-open .modal-container { overflow-y: scroll; } @media (max-width: 979px) { .modal-open .page-container .navbar-fixed-top{ overflow-y: visible; } } 
 <div class="page-container"> <div class="container"> <br /> <button class="btn launchConfirm">Launch Confirm</button> </div> </div> <div id="confirm" class="modal hide fade"> <div class="modal-body"> Do you want to continue? <input id="txtDemo" type="text" value="Jayesh" class="form-control"/> </div> <div class="modal-footer"> <button type="button" data-dismiss="modal" class="btn btn-primary" data-value="1">Continue</button> <button type="button" data-dismiss="modal" class="btn" data-value="0">Cancel</button> </div> </div> 

Sam you can try the following javascript code:-

    var screen_name = '',
    old_message = '',
    new_message = '';
    $("#msg").on('shown', function() {
        screen_name = $("button[data-target='#msg']").data("screenname"),
        old_message = $.trim($('#Message').val()),
        new_message = ( old_message.length == 0 ) ? screen_name : old_message; 
       $("#Message").focus().val(new_message + " "); 
   });

Let me explain why i have used this code. From the js fiddle link that you have provided what i understand is you are using the bootstrap 2 version, but the code that you are using

$("#msg").on('shown.bs.modal', function(){
  ...
}

was introduced only in bootstrap 3. so I have changed it to

$("#msg").on('shown', function(){
...
}

and the line

$("#Message").focus().val(new_message + " ");

is used so that you can set the focus after the custom text.I hope this will resolve your issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM