tags:

views:

1293

answers:

5

How to use jQuery to change the backgound color of a textbox? Thanks a lot!!!

+5  A: 

Better practice is to seperate UI from logic, in your case:

$("#textboxid"). addClass("aClass");


If you really need it your way, then do the following:

$("#textboxid").css({"background-color": "color"});

Replace #textboxid with the desired selector, and color with the desired color.

Note the following does the same for one property:

$("#textboxid").css("background-color", "color");
Dykam
No need for hard coded presentation in js. Use Css for style, js for behaviour.
redsquare
Fixed, it's indeed better.
Dykam
+1  A: 
$('#txtBoxID').css('background-color', '#ffff00');
Joe Davis
+6  A: 

Better to add a class name to the input rather than hard coding styles into your js. Presentation styles should reside in css not js.

$('#inputId').addClass('someCssClass');
redsquare
And that is also what your are doing now. I would say you should add an semantic class, not backgroundRed, I see that as an antipattern. And maybe the code here is UI code.
Dykam
it was just a sample....of course you should not have classes for each color. geee
redsquare
You still should not hard code style into js. What happens when he wants a border, then a different font. End up with an ugly mess of js.
redsquare
Ah, you modified the example.But for UI JS can it be appropriate.
Dykam
js for behaviour, not for adding css. Why make it more difficult that addClass?
redsquare
The "why" is the semantic portion right? so `addClass('highlight');` would be an appropriate thing for javascript to do, and then the stylesheet would specify what that looks like. Now, you could replace "highlight" with any random description, but the function is the same.
DGM
But your original answer showed none of these thoughts. Now your the expert;)
redsquare
A: 

Nice tutorial. very helpfull for me as a newbie jquery..

Thanks

Heri
A: 

thanks it helps me a lot

alex : from Nicaragua : SIF.SA

alex