JavaScript getAdjacentText method

JavaScript getAdjacentText method returns the text adjacent to the specified text element.

JavaScript getAdjacentText method

JavaScript getAdjacentText method

     

JavaScript getAdjacentText method returns the text adjacent to the specified text element.

 

 

 

 

 

Syntax: 

 textElement.getAdjacentText(position);

If position value is beforeBegin then the returned text would be the text immediately before the textElement. If position is afterBegin  then the returned text would be the text after the start of the textElement but before all other content in the textElement. If  position is beforeEnd then the returned text would be the text before the end of the textElement but after all other content in the textElement and if position value is afterEnd then the returned text would be the text after the end of the textElement

Description of code

In the following sample code we have written some text paragraph and the text on which we want to apply the method getAdjacentText() we have shown in the italic manner. We have created a button "Get Adjacent" which calls the function getAdjacent() as defined in the JavaScript. This function takes the text element by using the method getElementById("element id") and it returns the text before the text element and write this returned text to the document. Here is the full source code getAdjacentTextExample.html as follows:

getAdjacentTextExample.html 

 <html>
<body>
<script language="JavaScript">
  function getAdjacent() {
  document.write(document.getElementById('italic')
   .getAdjacentText('beforeBegin'));
  }
</script>
<div style="background: #cf2255; width:'100%';" align="center">
<font color="#05ff20" size="12pt">
<b>Focus Example</b>
</font>
</div>
<center>
<div style="background: #ffffcc; width:'100%';" align="center">
<p> &nbsp; </p>
<p>
<font color="#0000ff">
Rose India Technologies Pvt. Ltd. is a global services company that ensures maximum returns by providing quality software solutions and services. The Indian based company provides services to several reputed institutional clients, in the domain of IT and IT enabled Technologies. We help in understanding the client requirements and offer customized solutions in various specialized areas like Web based Technologies, Database Systems,<i id="italic"><b> Client Server Architecture </b></i>, E-commerce Solutions and Consultancy Services. Other services offered include Online Technical Tutorials, Content Development and Generation, Web solutions for B2B, B2C, C2C and Travel portals, Web Development, Promotion and Hosting, and Applications Service Provider Solutions.
</font>
</p>
</div>
<div style="background: #990999; width:'100%';" align="center">
<input type="button" value="Get Adjacent !" onclick="getAdjacent();"/>
</div>
</body>
</html>

Output :

Download Source Code