How do we use jQuery Attribute selector with a period?

jQuery Attribute selector is used to select elements with the specified attribute and value. You can use it with a period (.) in attribute values by utilizing appropriate attribute selector operators like *= (contains), ^= (starts with), or $= (ends with).

Syntax

The basic syntax for using jQuery attribute selector with a period is ?

$("[attribute*='.']")     // Contains a period
$("[attribute^='.']")     // Starts with a period  
$("[attribute$='.']")     // Ends with a period

Example

You can try to run the following code to learn how to use jQuery attribute selector with a period ?

<!DOCTYPE html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
            // Select elements with attribute containing a period
            $("div[myattr*='.']").css("background-color", "yellow");
            
            // Select elements with attribute starting with a period
            $("div[myattr^='.']").css("border", "2px solid red");
        });
    </script>
</head>
<body>
    <h3>jQuery Attribute Selector with Period Example</h3>
    
    <div id="sub1" myattr="java.subject">Java (contains period)</div>
    <div id="sub2" myattr=".htmlsubject">HTML (starts with period)</div>
    <div id="sub3" myattr="rubysubject">Ruby (no period)</div>
    <div id="sub4" myattr="python.programming.language">Python (multiple periods)</div>
</body>
</html>

The output of the above code is ?

Elements with periods in their myattr values will have:
- Yellow background (all elements containing a period)
- Red border (elements starting with a period)

Java, HTML, and Python divs will be highlighted, while Ruby remains unchanged.

Conclusion

jQuery attribute selectors work seamlessly with periods in attribute values using operators like *=, ^=, and $= to match different patterns containing periods.

Updated on: 2026-03-13T18:13:32+05:30

344 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements