How to toggle attribute

How to toggle attribute

Hi,
I need to create a button which shows and hide the password inside an input field.
I wrote this code which works partially:

  1. $('.show-hide-password').click(function(e) {
  2.       $(this).text(($(this).text() === 'Hide' ? 'Show' : 'Hide')); // Change the text
  3.       $('.engage-password__field').attr('type', 'text');
  4.       e.preventDefault();
  5.  });
If I click on "Show" button it shows the password. But it should hide the password by second click. So I need something to toggle the attribute each time I click the button.

How can I do this?

Thanks