开发者

dynamic textbox jquery focus

开发者 https://www.devze.com 2023-01-27 08:28 出处:网络
i am creating a dynamic text box of input type with the help of following code i am not able to set the focus on the input type.

i am creating a dynamic text box of input type with the help of following code i am not able to set the focus on the input type.

var elem = document.createElement("input");
elem.type = "text";
elem.id = "txtParent";
elem.setAttribute('onblur', 'SetSpanValueForParent("' + spnText.id + '")');
$(elem).focus();
$(spnText).append(elem);

i have also tried doing this elem.focus();

can u provide that one line statement how ca开发者_运维知识库n i achieve this


You first need to append the input, before setting the focus().

Elements can have focus only if they are visible.

Example:

function fx(spnText)
{
  var elem = document.createElement("input");
  elem.type = "text";
  elem.id = "txtParent";
  elem.setAttribute('onblur', 'SetSpanValueForParent("' + spnText.id + '")');
  $(spnText).append(elem);
  //a little delay before setting the focus
  setTimeout(function(){elem.focus()},50);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号