jQuery Selectors: Select elements that have attribute $(‘[attr]‘)

This entry is part 12 of 33 in the series jQuery Selectors

Using syntax $(‘[attr]’) we can select elements that have attribute attr.

<!DOCTYPE html>
<head lang='en'>
    <meta charset='UTF-8'>
    <script src='//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js'></script>
        $(function () {
            $('[type]').click(function () {
        <legend>Contact information</legend>

        <label for='name'>Name:</label>
        <input type='text' name='name' id='name'>
        <label for='phone'>Phone:</label>
        <input type='text' name='phone' id='phone'>
        <label for='comment'>Comments:</label>
        <textarea name='comment' rows='3' id='comment'></textarea>
        <input type='submit' value='Send'>
        <input type='reset' value='Clear'>
<p class='message' style='display: none; color: grey'>Button is clicked</p>


We can be more specific and use $(‘E[attr]’).

Series Navigation<< jQuery Selectors: Attribute attr not equal to a value
jQuery Selectors: Matches the end of the attribute value E[attr$=value] >>

Leave a comment

Leave a Reply