0

I am currently learning window.onload and document.onload as attributes on html. But i fail to use them properly. In my code i want to focus a specific text area named "emri", using the document.onload but without success. Some would say to use it as a script or smth, but i want to use it as an attribute inside the tag. What am i doing wrong?

 <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous"> <link rel="stylesheet" href="App.css"> <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ" crossorigin="anonymous"> </head> <body onload="document.my-form1.emri.focus();" > <main class="my-form"> <div class="container"> <div class="row justify-content-flex-start"> <div class="col-md-6"> <div class="card"> <div class="card-header">Apliko</div> <div class="card-body"> <form name="my-form1" onsubmit=" return formValidation();"> <div class="form-group row"> <label for="emri" class="col-md-3 col-form-label text-md-right">Emri juaj:</label> <div class="col-md-6"> <input type="text" class="form-control" name="emri"> </div> </div> 
1
  • Try with <body onLoad="document.getElementById('emri').focus();"> Commented May 30, 2019 at 9:59

4 Answers 4

1

Here your updated html :

<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport"	content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="stylesheet"	href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css"	integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB"	crossorigin="anonymous"> <link rel="stylesheet" href="App.css"> <link rel="stylesheet"	href="https://use.fontawesome.com/releases/v5.2.0/css/all.css"	integrity="sha384-hWVjflwFxL6sNzntih27bfxkr27PmbbK/iSvJ+a4+0owXq79v+lsFkW54bOGbiDQ"	crossorigin="anonymous"> </head> <body onload="document.getElementById('emri').focus();">	<main class="my-form">	<div class="container">	<div class="row justify-content-flex-start">	<div class="col-md-6">	<div class="card">	<div class="card-header">Apliko</div>	<div class="card-body">	<form name="my-form1" onsubmit=" return formValidation();">	<div class="form-group row">	<label for="emri" class="col-md-3 col-form-label text-md-right">Emri	juaj:</label>	<div class="col-md-6">	<input type="text" class="form-control" name="emri" id="emri"/>	</div>	</div>	</form>	</div>	</div>	</div>	</div>	</div>	</main> </body> </html>

Sign up to request clarification or add additional context in comments.

Comments

0

You can just add the autofocus attribute to the input.

<input type="text" class="form-control" name="emri" autofocus> 

2 Comments

If he is using HTML5
also he will need to add id to input so <body onLoad="document.getElementById('emri').focus();"> to work. @GovindParashar
0

There is nothing wrong how you use `window.onload´. It is the name of the form. Since there is a "-" character in the name, you should use it like this:

<body onload="document['my-form1'].elements.emri.focus();"> 

see: Example

Comments

0

You need to select like this

<body onload="document.getElementsByName('emri')[0].focus();"> 

If you are using HTML5, you can use like autofocus attribute in input.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.