Monday 10 April 2017

jQuery attribute Selector

  • The [attribute] selector selects each element with the specified attribute.
Syntax

$('[attribute]')
$('[attribute="value"]')

Ex:
$('[title]') :-  Selects all elements that have title attribute.
$('div[title]'):-  Selects all div elements that have title attribute.
$('[title="divtitle"]') :-  Selects all elements that have title attribute value - divtitle.
$('div[title="divtitle"]') :-  Selects all div elements that have title attribute value - divtitle.
$('[title^='Ajay']') :- Selects  all elements with a title attribute value starting with "Ajay".
$("[title~='welcome']") :- Selects  all elements with a title attribute value containing the specific word "welcome".
$("[title*='welcome']") :- Selects all elements with a title attribute value containing the word "welcome".


$('[title]') :-  Selects all elements that have title attribute.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<script type="text/javascript">
        $(document).ready(function () {
            $('[title]').css('border', '5px solid red');
        });
    </script>

  </head>
      <body>    
      <div title="dvTitle">
        DIV with Title
    </div>
    <br />
    <div title="dvTitle1">
        DIV with another Title
    </div>
      <span title="spanTitle">
        SPAN element
    </span>
    <p>I live in Mumbai.</p>
    </body>
</html>

output:



$('div[title]'):-  Selects all div elements that have title attribute.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

<script type="text/javascript">
        $(document).ready(function () {
            $('div[title]').css('border', '5px solid red');
        });
    </script>

  </head>
      <body>    
      <div title="dvTitle">
        DIV with Title
    </div>
    <br />
    <div title="dvTitle1">
        DIV with another Title
    </div>
      <span title="spanTitle">
        SPAN element
    </span>
    <p>I live in Mumbai.</p>
    </body>
</html>

output:

No comments:

Post a Comment

Thank you for comment