focus on a textbox created by ajax
I have writen an ajax page that will change a particular div content. In that the ajax code will create a textbox. Now I would like to focus on that textbox after calling that function. on clicking a button the function will get called as
function addnewitem5()
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("aa433").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","ajax-production.php?type=sales",true);
xmlhttp.send();
}
on the ajax-production.php the code is just
<?php echo '<input type="text" name="barcode" id="barcode"/>';?>
How can 开发者_JS百科I focus on that textbox?
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("aa433").innerHTML=xmlhttp.responseText;
document.getElementById("textboxname").focus();
}
by this you can set focus on text box..
After the innerHtml you can add focus by .focus()
document.getElementById("aa433").innerHTML=xmlhttp.responseText;
document.getElementById("barcode").focus();
Don't use innerHTML, it's an unreliable proprietary Microsoft method and NO just because someone thought to add it to the HTML5 spec does NOT make it legitimate to use.
The problem is that innerHTML works just enough to make you THINK it is working. When using it in write-mode it essentially just throws the HTML where you specify though it doesn't correctly register the nodes. So basically when you want to crown a queen you need to know the proper etiquette to be used at every single step, innerHTML chucks the crown at her face.
Use standard DOM methods such as appendChild, insertBefore or importNode.
Also avoid using responseText, XHTML isn't text, it's XML and you should stick to using responseXML instead. When you make these changes you'll be able to work with AJAX loaded content as if AJAX wasn't involved to begin with.
精彩评论