Javascript Reference - HTML DOM Anchor password Property








The password property sets or gets the password part of the href attribute value.

Example: https://userName:password@www.example.com.

Browser Support

password Yes No Yes No Yes

Syntax

Return the password property:

var v = anchorObject.password 

Set the password property:

anchorObject.password = password;

Property Values

Value Description
password Set the password part of a URL




Return Value

A String representing the password part of the URL.

Example

The following code shows how to get the password part of a link.


<!DOCTYPE html>
<html>
<body>
<!--from w w w . j  a va2s . c  o m-->
<p><a id="myAnchor" href="https://userName:password@www.example.com">Example link</a></p>
<button onclick="myFunction()">test</button>
<p id="demo"></p>

<script>
function myFunction() {
    var x = document.getElementById("myAnchor").password;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to change the password part of a link


<!DOCTYPE html>
<html>
<body>
<!-- w w w  .j  ava  2s  . c om-->
<p><a id="myAnchor" href="https://userName:password@www.example.com">Example link</a></p>

<button onclick="myFunction()">test</button>

<p id="demo"></p>

<script>
function myFunction() {
    document.getElementById("myAnchor").password = "Password101";
    document.getElementById("demo").innerHTML = "changed";
}
</script>

</body>
</html>

The code above is rendered as follows: