Mat Tailor
Mat Tailor

Reputation: 3

How to put the focus on Textbox

I'm working on a chat program. (javascript/html/css) How can I put the focus on the field where i want to write my message?

Normally it's css code, isn't it?

The css of my message field looks like this right now:

Code:

#messageField {
  width: 600px;
  margin: 20px auto;
  border: 1px solid #5a2300;
}

What do I have to add to the cursor there when my app starts?

Greetings

Upvotes: 0

Views: 84

Answers (2)

planetjones
planetjones

Reputation: 12633

<head>
  <script type="text/javascript">
    function setFocus() {
      document.getElementById("messageField").focus();
    }
  </script>
</head>

<body onload="setFocus()">
</body>

Upvotes: 3

Roland
Roland

Reputation: 9701

If the id is bind to an input or textarea element, you can simply add this:

#messageField:focus {
    width: 600px;
    margin: 20px auto;
    border: 1px solid #5a2300;
}

Upvotes: 0

Related Questions