javascript - How do I focus a button inside modal when the modal is opened - Stack Overflow

When call modal popup I need to focus on the first button on the modal.The button is dynamically appen

When call modal popup I need to focus on the first button on the modal.
The button is dynamically appended to the modal footer

var difVal = ( totalAmnt - payAble ).toFixed(2);         
$('#chqMsgContent').html( 'Total Diffrent than the Recive Amount. ( ' + difVal + ' )</br> (Please type manually for over payment) ');        
$('#myModal').modal('show');
$('.modal-dialog').css('width', '350');     
var btnsForModl = '<input type="button" class="btn  btn-xs ok" id="okDif" data-dismiss="modal" value="Ok" data-value="Cash" /> ';       
$('.modal-footer').html(btnsForModl);   

$('#okDif').focus();                

$('#okDif').focus(); not worked than I have tried this on

$('#myModal').on('shown', function () {
     alert($('#okDif').val());
     $('#okDif').focus();
}); 

Both of the methods are not working, but the popup window is working fine.

JSFiddle demo

When call modal popup I need to focus on the first button on the modal.
The button is dynamically appended to the modal footer

var difVal = ( totalAmnt - payAble ).toFixed(2);         
$('#chqMsgContent').html( 'Total Diffrent than the Recive Amount. ( ' + difVal + ' )</br> (Please type manually for over payment) ');        
$('#myModal').modal('show');
$('.modal-dialog').css('width', '350');     
var btnsForModl = '<input type="button" class="btn  btn-xs ok" id="okDif" data-dismiss="modal" value="Ok" data-value="Cash" /> ';       
$('.modal-footer').html(btnsForModl);   

$('#okDif').focus();                

$('#okDif').focus(); not worked than I have tried this on

$('#myModal').on('shown', function () {
     alert($('#okDif').val());
     $('#okDif').focus();
}); 

Both of the methods are not working, but the popup window is working fine.

JSFiddle demo

Share Improve this question edited Sep 12, 2015 at 8:44 Artur Filipiak 9,1574 gold badges32 silver badges56 bronze badges asked Sep 12, 2015 at 6:22 Nasik AhdNasik Ahd 7981 gold badge10 silver badges24 bronze badges 4
  • can you post a fiddle jsfiddle? – kurt Commented Sep 12, 2015 at 6:35
  • there is a big process to get the pop up in my system is it possible to post the plete page on jsfiddle – Nasik Ahd Commented Sep 12, 2015 at 7:07
  • jsfiddle/nsk21/DTcHh/12012 – Nasik Ahd Commented Sep 12, 2015 at 7:26
  • Ok this has me stumped. There something weird going on – kurt Commented Sep 12, 2015 at 7:52
Add a ment  | 

2 Answers 2

Reset to default 3

Anything inside the modal is initially hidden, therefore the input element (as hidden) cannot have focus on.
If you don't need the modal to be animated, you could simply remove the fade class of the modal, so that the modal content is visible right away after you call .modal('show').
JSFiddle


The other option which you have tried won't work because bootstrap modal has no event called shown.
It has a shown.bs.modal event:

$('#myModal').on('shown.bs.modal', function(e){
    $('#okDif').focus();
});

JSFiddle

You need to add a slight timeout for the focus mand, as otherwise it fires before the element is present.

This works:

$(document).on('click', '#on', function(){  
  $('#chqMsgContent').html('Total Diffrent than the Recive Amount');         
  ...
  setTimeout(function(){ 
    $("#okDif").focus();
  }, 150);
});

DEMO

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744371741a4570988.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信