简体   繁体   中英

How to find whether the class attribute is present or not in the input element using Jquery

I need to find the class name specified in the input attribute using jquery

<Html>
<body>
<input type="text" class="name"/>
</body>
</Html>

here how i can find the class name using jquery

Try .attr() :-

$("input[type='text']").attr('class');

DEMO

 $('input').hasClass('name') ? alert('has class name') : alert('has no class name') 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" class="name"/> 

use .hasClass

Description: Determine whether any of the matched elements are assigned the given class.

Try this:

 var className = $('.name').attr('class'); alert(className); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" class="name"/> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM