Home » JavaScript Tutorial

focus() Method with Example in JavaScript

JavaScript | focus() Method: Here, we are going to learn about the focus() method with example in JavaScript.
Submitted by Siddhant Verma, on January 10, 2020

JavaScript | focus() Method

The focus() method in DOM is used to give focus to an element. Its counterpart is blur() method which removes that particular element from focus. It does not take any parameters and doesn't have a return type.

Example:

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Focus method</title>
    <!-- Compiled and minified CSS -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/css/materialize.min.css">

    <!-- Compiled and minified JavaScript -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0/js/materialize.min.js"></script>

</head>
<style>
    body {
        background: palegreen;
    }
    
    form {
        position: absolute;
        top: 50px;
        left: 250px;
        border: 2px solid white;
        background: whitesmoke;
        text-align: center;
        padding: 12px;
        margin: 12px;
        width: 700px;
    }
</style>

<body>
    <div class="container">
        <form action="">
            <input type="text" name="Name" id="name" placeholder="Name">
            <input type="email" name="Email" id="email" placeholder="Email">
            <input type="password" name="Password" id="password" placeholder="Password">
            <button class="btn">Submit form!</button>
        </form>
    </div>
</body>
<script>
</script>

</html>

Output

JavaScript | focus() method | Example 1

You can see that materialize styles its forms in such a way that when you click an input field, a blue underline appears underneath that field.

JavaScript | focus() method | Example 2

On clicking the password field, there’s a blue underline indicating that the field is in focus. We can set a field to focus without clicking it by triggering it an onmousemove event.

<input type="text" name="Name" id="name" placeholder="Name" onmousemove="focus()">
JavaScript | focus() method | Example 3

Now, when I move my mouse over the name input field, the onmousevent gets triggered and it fires the focus function due to which we see the little blue underline on the field.






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.