We will get an example here and will learn how we can select an element by attribute name in jQuery.
We will use jQuery element selector to select element by attribute, Like we use #id to select an element by id .classname to select an element by class similarly we can use [attribute=’value’] to select an element by attribute.
Html Code Example 1
<div id="mydiv"> This is my div with id </div>
<div class="mydiv"> This is my div with class </div>
<div attr="mydiv"> This is my div with attribute name attr </div>
jQuery Code
$("#mydiv").html(); // Will print all data from the div by id selector.
$(".mydiv").html(); // Will print all data from the div by cass selector.
$("[attr='mydiv']").html(); // Will print all data from the div by attribute selector.
Html Code Example 2
<input name="name" value="John"/>
<input employee="name" value="John Deo"/>
<input employee="address" value="Delhi"/>
JQuery Code
$("[name='name']").val(); // output John
$("[employee='name']").val(); // output John Deo
$("[employee='address']").val(); // output Delhi
Thanks.