Javascript Reference - HTML DOM IFrame name Property








The name attribute from iframe element specifies the name of an iframe.

We have use the name value in the following ways.

  • We can use the name value to reference the iframe element in a JavaScript.
  • We can use the name value in the target attribute of an <form>
  • We can use the name value in the formtarget attribute of an <input> element
  • We can use the name value in the formtarget attribute of an <button> element

The name property sets or gets the value of the name attribute in an iframe element.





Browser Support

name Yes Yes Yes Yes Yes

Syntax

Return the name property.

var v = iframeObject.name

Set the name property.

iframeObject.name=name

Property Values

Value Description
name The name of the iframe




Return Value

A String type value representing the name of the iframe.

Example

The following code shows how to Change the name of an iframe.


<!DOCTYPE html>
<html>
<body>
<iframe id="myFrame" src="http://example.com" name="iframe_a"></iframe>
<p id="demo"></p>
<button onclick="myFunction()">test</button>
<!--  ww  w . ja va 2  s  . com-->
<script>
function myFunction() {
    document.getElementById("myFrame").name = "newIframeName";
    document.getElementById("demo").innerHTML = "changed";
}
</script>
</body>
</html>

The code above is rendered as follows:

Example 2

The following code shows how to get the name of an iframe.


<!DOCTYPE html>
<html>
<body>
<!--from   w ww . ja  v  a  2  s  .  co m-->
<iframe id="myFrame" src="http://example.com" name="iframe_a"></iframe>
<p id="demo"></p>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    var x = document.getElementById("myFrame").name;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: