how to filter textbox and button in jquery

Posted by Klbaiju under jQuery on 4/4/2014 | Points: 10 | Views : 791 | Status : [Member] | Replies : 1
Hi,
In my application ,i want check whether the controls are textbox,password and buttons.
if it is textbox and password, i want to filter and add a class
i try following example
if ($('input').filter(':not (submit)').val().length > 0) {
/
$('input').filter(':not (submit)').addClass("validate");
}
i want to add class to textbox and password controls only and avoid buttons.

How it is possible.

Regards
Baiju




Responses

Posted by: A2H on: 4/6/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hi,
You can use the below code to find all the textboxes and password type textboxes in your page.
$("input[type='text'], input[type='password']").addClass('validate');

Above code will add class to only textboxes and password.

Please find the complete implementation
HTML Mark Up:

 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
<asp:Button ID="Button2" runat="server" Text="Button" />
<asp:Button ID="Button3" runat="server" Text="Button" />

Sample Css Style:
.validate
{

color:red;

}

JQuery Function to add class to all textboxes
$(function() {
$("input[type='text'], input[type='password']").addClass('validate');
});


You can find a sample working demo in this link
http://jsbin.com/karizera/1/edit?html,css,js,output

Thanks,
A2H
My Blog

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response