Rajasri.J
Rajasri.J

Reputation: 158

Set focus to an input field without blinking cursor

I have a textfield (fullname) and on click of the button "Set Focus", i want the focus to be set on the textfield(fullname) but do not want the blinking cursor within the focused textfield. How can this be achieved using js/jQuery.

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Customer Details</title>    
</head>
<body>    
    <input type="text" id="fullname" placeholder="Name">      
    <button type="button" onClick="document.getElementById('fullname').focus()" id="button">Set Focus</button>
</body>
</html>

I have tried using both jquery and js focus(). But in both the cases, i could see blinking cursor in the focused textfield. I saw a solution which requires me to alter the text-shadow, border and outline to achieve this. But i want to retain the border and outline for the focused textfield

Upvotes: 3

Views: 4237

Answers (3)

A. Wolff
A. Wolff

Reputation: 74420

This trick seems to work on all major browser, keeping outline\border and hiding blinking caret:

#fullname:focus{
    text-indent: -9999em;
    text-shadow : 9999em 0 0 #000;
}
<input type="text" id="fullname" placeholder="Name" />      
    <button type="button" onClick="document.getElementById('fullname').focus()" id="button">Set Focus</button>

Upvotes: 6

Deepak
Deepak

Reputation: 65

Hi i have made one Hack hope this will help :D

<div style="position:relative">
    <input type="text" id="txt" /> 
    <span id="txt2"></span>
</div>

var txt = $('#txt');
txt.focus()
$('#txt').on('keyup', function () {
    var val = $(this).val();
    //alert(val);
    $('#txt2').text(val)
})


#txt:focus {
    outline:none
}
*[id^="txt"] {
    position:absolute;
    outline:none 
}
#txt {
z-index:12;
background:#ddd;
opacity:0.0;
width:200px;
height:30px
}
#txt2 {
height:30px;
border:1px solid #ddd;
display:block;
width:200px;
word-wrap: break-word;
overflow:hidden;
line-height:30px
}

Here is the link please have a look

here : what i am doing is i have made one input and a span inside a relatively positioned div and then i have managed its children with absolute position on each other and made input transparent after this i have use keyp function and taking value of input and changing text of span ..hope it will help

Upvotes: 0

Jrey
Jrey

Reputation: 1638

Here's what you want? see my code, apply it :) it will focus input field when the button is clicked. Happy Coding.

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>Customer Details</title>    
    <style>
#fullname {
border: none;
    color: transparent;
    display: inline-block;
    font-size: 2em;
    text-shadow: 0 0 0 gray;
    width: 2em;
width: 200px;
border: 1px solid black;
}
#fullname:focus {
        outline: none;
    }
</style>
</head>
<body>    
    <input type="text" id="fullname" placeholder="Name">      
    <button type="button" onClick="document.getElementById('fullname').focus()" id="button">Set Focus</button>
</body>
</html>

Upvotes: 1

Related Questions