You are here: Reference > JavaScript > client-side > HTML DOM > methods > getAdjacentText

getAdjacentText method

Browser support:
Returns the contents of the TextNode located at the specified position relative to the current element.

Syntax:

object.getAdjacentText (where);
You can find the related objects in the Supported by objects section below.

Parameters:

where
Required. String that specifies the position where the TextNode is located.
One of the following values:
beforeBegin
The TextNode is the previous sibling of the current element.
afterBegin
The TextNode is the first child of the current element.
beforeEnd
The TextNode is the last child of the current element.
afterEnd
The TextNode is the next sibling of the current element.

Return value:

Returns the contents of the TextNode element as a string. If no TextNode element is located at the specified position, it returns an empty string.

Example HTML code 1:

This example illustrates the use of the getAdjacentText method:
<head>
    <script type="text/javascript">
        function FindText (text){
            var from = document.getElementById ("searchFrom");
            alert (from.getAdjacentText(text));
        }
    </script>
</head>
<body>
    far from ...
    <br />
    last text before
    <div id="searchFrom" style="border:1px solid black">
        first text inside
        <br />
        inside
        <br />
        last text inside
    </div>
    first text after
    <br />
    far from ...
    <br />

    <button onclick="FindText ('beforeBegin')">Find text before begin</button>
    <button onclick="FindText ('afterBegin')">Find text after begin</button>
    <button onclick="FindText ('beforeEnd')">Find text before end</button>
    <button onclick="FindText ('afterEnd')">Find text after end</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content