Change CSS properties in focus

Is there a way to change the border of the CSS div when the input field gets focus (for example, when it loses focus, removes the border).

HTML:

<input type="text" name="title" id="title"></input>
<div id="mainTitle">The Main Title</div>

CSS

#mainTitle {
    border: 1px solid #ccc; 
}
+3
source share
2 answers
$(function() {      //aka document.ready
    $("input#title").focus( function() {
        $("div#mainTitle").css("border","1px solid #ccc");
    });

    $("input#title").blur( function() {
        $("div#mainTitle").css("border","");
    });
});
+10
source

You could just use css if your users are working in a competent browser (like that, and not in IE, for the most part):

input:hover + #mainTitle,
input:focus + #mainTitle,
input:active + #mainTitle {
   border: 2px solid #ccc;
}
+3
source

Source: https://habr.com/ru/post/1774704/


All Articles