Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery focus and select on id input field

DEMO FIDDLE

<script>
$(document).ready(function ()
{  
     document.getElementById(introdukt).focus()
    document.getElementById(introdukt).select()
});
</script>
<input type="text" size="12" id="introdukt" value="match" style="text-align:center"  />

Does not focus on the field? How to correct this?

like image 390
secr Avatar asked Jul 19 '13 14:07

secr


3 Answers

working fiddle

$(document).ready(function ()
{  
    $('#introdukt').focus()
    $('#introdukt').select()

    //or if you want to be more efficient and use less characters, chain it
    $('#introdukt').focus().select()

});

your weren't using selectors right. check my fiddle.

also, I changed the id in your fiddle back to introdukt from what you had.

also, if you're going to use jquery document ready you may as well use the jquery selector instead of the pure js method.

like image 73
Rooster Avatar answered Nov 19 '22 16:11

Rooster


You should chain your method calls since jQuery will only need to look for the element once.

$(document).ready(function ()
{  
    $('#introdukt').focus().select();
});
like image 11
Derick Avatar answered Nov 19 '22 15:11

Derick


You need some quotes in there:

document.getElementById('introdukt').focus()
document.getElementById('introdukt').select()

Here's a working Fiddle (Although you've used a different ID on there..?)

like image 1
George Avatar answered Nov 19 '22 16:11

George