Add a class depending on the attribute in jquery
I'm trying to add a class to some input elements depending 开发者_运维技巧on the type, so if it's type='text' iI want to add .txt class and if it's typt='button' I want to add .btn class. I tried some stuff but so luck, it adds .text to all of them:
var text_type = $('#right input').attr('type');
if(text_type=='text'){
$('#right input').addClass('text');
}
else if(text_type=='button'){
$('#right input').addClass('btn');
};
How do I achieve that? Thanks in advance.
Mauro
$('#right input[type="text"]').addClass('text');
$('#right input[type="button"]').addClass('btn');
Attribute equals selector.
I think what you want to do is something like this were you add the class onto all inputs which have a type of text.
$("input[type='text']").addClass("text");
精彩评论