-->

28 April 2021

jQuery Attribute Selector

  Asp.Net CS By Example       28 April 2021
jQuery To Select Elements by Attribute

 In this post, we are learning about jQuery To Select Elements by Attribute selector. jQuery allow we select page elements based on their attributes, which enables we to differentiate elements of the same type. To select elements based on their attributes,use the below selector.

 [attribute] 

 where attribute is the attribute we are searching for.


 For example, to match the <p> elements that have a language attribute, use below selector:

 $('p[language]') 


 Below code example puts for see above jQuery selecting elements by attribute selector working.

jqscript.html
<html>
<head>
    <title>
        Selecting elements by
        attribute
    </title>
    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript">
        function setStyle() {
            $('p[language]').css("font-style", "italic");
        }
    </script>
</head>
<body>
    <h1>
        Selecting elements by
        attribute
    </h1>
    <div>
        <p>This is paragraph 1.</p>
        <p>This is paragraph 2.</p>
        <p language="English">This is paragraph 3.</p>
        <p>This is paragraph 4.</p>
    </div>
    <form>
        <input type="button"
               value="Select"
               onclick="setStyle()"
        </input>
    </form>
</body>
</html>


Output:

logoblog

Thanks for reading jQuery Attribute Selector

Previous
« Prev Post

No comments:

Post a Comment

Please do not enter any spam link in the comment box.