...

View Full Version : Need help with Jquery to set background color on focus for elements created on demand



asifakhtar
08-13-2010, 03:55 PM
Hi,
I am Jquery to set background color on focus. It is working fine for existing elements but it is not working for the elements(input, textarea, select) that I create on demand. How can I fix this?

My Jquery code is :
<script>
$(document).ready(function()
{
$("input, textarea,select").focus(function()
{
$(this).addClass("activeField").removeClass("idle");
}
).blur(function()
{
$(this).removeClass("activeField").removeClass("idle");
});
});
</script>
Thanks

SB65
08-14-2010, 08:08 AM
You say you're creating elements on demand - have they been created by the time your code runs?

asifakhtar
08-14-2010, 04:58 PM
You say you're creating elements on demand - have they been created by the time your code runs?
Thank u for the rely. No.

SB65
08-14-2010, 07:43 PM
Thank u for the rely. No.

That's why it has no effect then. You are changing classes on elements which don't exist yet.

One approach would be to wrap your posted code into a function and call the function every time you create the new elements.

Spudhead
08-16-2010, 12:50 PM
You need live() (http://api.jquery.com/live/)


$("input, textarea,select").live('focus blur', function(event) {
if (event.type == 'focus') {
$(this).addClass("activeField").removeClass("idle");
} else {
$(this).removeClass("activeField").removeClass("idle");
}
});

SB65
08-16-2010, 02:33 PM
You need live() (http://api.jquery.com/live/)



That's a much better approach.

asifakhtar
08-16-2010, 02:42 PM
You need live() (http://api.jquery.com/live/)


$("input, textarea,select").live('focus blur', function(event) {
if (event.type == 'focus') {
$(this).addClass("activeField").removeClass("idle");
} else {
$(this).removeClass("activeField").removeClass("idle");
}
});
Thank u very much for ur response but ur code is not doing anything.

Spudhead
08-16-2010, 04:47 PM
My bad. Event type should be "focusin":


if (event.type == 'focusin') {

asifakhtar
08-16-2010, 05:46 PM
My bad. Event type should be "focusin":


if (event.type == 'focusin') {

Thank u. It works.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum