$("form :input").focus(function() {
$("label[for='" + this.id + "']").addClass("labelfocus");
}).blur(function() {
$("label").removeClass("labelfocus");
});
Home / Code Snippets / jQuery Code Snippets / Highlight Related Label when Input in Focus
Highlight Related Label when Input in Focus
Chris Coyier
on
Need front-end development training?
Frontend Masters is the best place to get it. They have courses on all the most important front-end technologies, from React to CSS, from Vue to D3, and beyond with Node.js and Full Stack.
pleasee give me this example :(
This will come in handy, will use a subtle rgba colour background on selection – cheers :-)
I ran a test case with this because i was doing this the long way. Come to find out, if you happen to already know the labels
for=""
and input’s id, then a switch case is quicker.jsPerf Benchmark Test Case (Slightly modified with triggering events). I do however prefer this method due to the less code and easy to use, especially if you have dynamic forms.
You can style it with just CSS if you put the label after the input
Here is very good example It realised like small jQuery plugin and can be customized as you wish.
I want same class on label when there is value on input, How to do that