attr() method in jQuery


The attr() method used to Get and Set attribute value of selected element.

Syntax
//get text alignment of paragraph
$("p").attr("align");

//set center alignment of paragraph
$("p").attr("align","center");

Example

The following example shows, how to show and hide password using jQuery attr() method.

Demo

 
<html>
  <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>   
  </head>
  <body>
    <input type='password' id='pwd' value='12345'>
    <input type='button' value='Show' id='btn'>
  <script>
    $(document).ready(function(){
      $("#btn").click(function(){
        var type=$("#pwd").attr("type");
        if(type=='password'){
          $("#pwd").attr("type","text");
          $(this).val("Hide");
        }else{
          $("#pwd").attr("type","password");
          $(this).val("Show");
        }
      });
    });
  </script>
  </body>
</html>
Try it Yourself