Reputation: 1657
I have a contact form. I want to add class to the parent div of the input box on the input:focus. Here is the fiddle. I want the input box and the div background color to change to purple on the input focus.
Markup:
<div class="round">
<div class="round_label">name</div>
<input type="text" class="round_text">
</div>
Javascript:
jQuery(document).ready(function($) {
$(".round_text:focus").parent().css('background', '#8b66ac');
$('.round_text:focus').parent().css('background-color', 'red');
});
CSS:
.round {
background: none repeat scroll 0 0 #F3F3F3;
border-radius: 30px 30px 30px 30px;
border-top: 1px solid #B2B2B2;
height: 50px;
padding-left: 20px;
width: 440px;
}
.round_text:focus {
background: none repeat scroll 0 0 #8b66ac;
}
.round_label {
border-right: 1px solid #D1D1D1;
color: #B6B6B6;
float: left;
font-size: 16px;
height: 40px;
padding-top: 10px;
width: 156px;
}
.round_text {
background: none repeat scroll 0 0 #F3F3F3;
border: medium none;
color: #424242;
float: left;
height: 50px;
width: 240px;
}
Also, Can you help me if I need to add two properties eg:
$(".round_text").focus(function(){
$(this).parent().css('background', '#8b66ac','color','#fff');
}).blur(function(){
$(this).parent().css('background', /*color*/);
})
Upvotes: 14
Views: 49994
Reputation: 728
You can add class instead and give the style
jQuery(document).ready(function($) {
$(".text").focus(function(){
$(this).parent().removeClass("round");
$(this).parent().addClass("bluebg");
}).blur(function(){
$(this).parent().removeClass("bluebg");
$(this).parent().addClass("round");
})
});
.bluebg {
background: none repeat scroll 0 0 #8b66ac;
color:#623886;
}
Upvotes: 24
Reputation: 171669
Use focus event
$(".round_text").focus(function(){
$(this).parent().css('background', '#8b66ac');
}).blur(function(){
$(this).parent().css('background', /*color*/);
})
EDIT: Alternate solution using toggleClass()
CSS
.round_text:focus, .round.is_focused {
background: none repeat scroll 0 0 #8b66ac;
}
JS
$(".round_text").on('focus blur', function(){
$(this).parent().toggleClass('is_focused');
})
Upvotes: 10
Reputation: 29005
If I am getting your question right,
You are selecting focused
elements, but you need to change css of parent div
on focus
event.
Do this,
jQuery(document).ready(function($) {
$(".round_text").focus(function(){
$(this).parent().css('background', '#8b66ac');
})
});
Upvotes: 3
Reputation:
Try this
$("input").on("focus",function(){
$(this).parent().addClass("any_class").css("background","purple");
});
Upvotes: 4