简体   繁体   中英

dynamic textbox jquery focus

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 can 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);
}

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