19

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?

VIVEK-MDU
  • 2,483
  • 3
  • 36
  • 63
secr
  • 624
  • 4
  • 9
  • 20

5 Answers5

34

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.

Rooster
  • 9,954
  • 8
  • 44
  • 71
  • 1
    no need to select twice - use chainging `$('#introdukt').focus().select();` – robisrob Jan 13 '16 at 18:03
  • @robisrob true. I honestly dont remember answering this, but I'd like to believe I did it like that to demonstrate more clearly. Anywho, I'll ammend the answer as it appears (based on you) that people actually use this :D – Rooster Jan 13 '16 at 18:18
11

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

$(document).ready(function ()
{  
    $('#introdukt').focus().select();
});
Derick
  • 161
  • 1
  • 6
1

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..?)

animuson
  • 53,861
  • 28
  • 137
  • 147
George
  • 36,413
  • 9
  • 66
  • 103
1

check the updated script in jsfiddle

HTML:

<input type="text" size="12" id="czasow1introdukt" value="match" style="text-align:center"/>

JS:

$(document).ready(function ()
{  
     document.getElementById('czasow1introdukt').focus()
    document.getElementById('czasow1introdukt').select()
});
Guna
  • 74
  • 11
Jothi Kannan
  • 3,320
  • 6
  • 40
  • 77
0

You can do it without using js, just with the autofocus html attribute like that :

<input autofocus type="text" size="12" id="introdukt" value="match" style="text-align:center"  />

Have a look to this fiddle : http://jsfiddle.net/wrKac/5/

Lucas Willems
  • 6,673
  • 4
  • 28
  • 45