• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
Actual Wizard

Actual Wizard

Great Content From Actual Wizards

  • Blogging
  • Programming
  • Social Media
  • Marketing
You are here: Home / Programming / JavaScript

JavaScript String Anchor Method Explained

August 13, 2020 by Kevin Marszalek

The HTML code to wrap text in an anchor tag can be generated in JavaScript using the anchor method.

MyHTML = MyText.anchor("anchor-name");

Important Note

The anchor method is not standardized and may not behave consistently in all browsers.

Using this method should be avoided if possible and here is one example of an alternative.

JavaScript Anchor String Method Example

<!DOCTYPE html>
<html lang="en">
<head>
	<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
	<meta content="utf-8" http-equiv="encoding">
	<title>JavaScript Anchor String Method Example</title>
</head>
<body>
<script>
	var MyText = "Sample Text With HTML Anchor";
	var MyHTML = MyText.anchor("js-anchor-example");
	document.write(MyHTML);
</script>
</body>
</html>

The code above produces a blank HTML document with the following HTML:

<a name="js-anchor-example">Sample Text With HTML Anchor</a>

Alternative to Using the Anchor Method

Even though the most commonly used browsers treat anchor method consistently, it’s still better to avoid potential issues by creating the needed HTML code inside a string and then inserting that HTML into the DOM.

Example JavaScript Anchor Method Alternative:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
	<meta content="utf-8" http-equiv="encoding">
	<title>JavaScript Anchor String Method Alternative</title>
</head>
<body>
	<h1>Generate HTML With Anchor Tag JavaScript Example</h1>
<script>
	var MyText = "Example of an alternative way to generate HTML anchor tags in JavaScript.";
	var MyHTML = "<a id=\"js-anchor-example\">" + MyText + "</a>";
	document.body.innerHTML += MyHTML;
</script>
</body>
</html>

Which will create the following HTML:

<a id="js-anchor-example">Example of an alternative way to generate HTML anchor tags in JavaScript.</a>

Using the Anchor Method With Out An Argument

If the anchor method of a string is called with out passing a string as an argument, the resulting HTML will default to having an anchor with the name attribute set to “undefined.”

The following code:

Var MyText = "Some Text";
MyHTML = MyText.anchor();
document.write(MyHTML);

Produces the following undesirable HTML code:

<a name="undefined">Some Text</a>

Filed Under: JavaScript

Read More From Actual Wizard

  • Understanding the Different Parts of an Email Address
    An email address has four parts; the recipient name, the @ symbol, the domain name, and the top-level domain. …
  • How to Get the Last Element of an Array in JavaScript
    There are a number of different ways to get the last element of an array in JavaScript. Below we will explore …
  • How to use document.write() in JavaScript
    How to use document.write() in JavaScript The document.write() function is commonly used when testing simple …
  • How to Open a New Tab with JavaScript
    Opening a new browser window in a new tab can be done easily in JavaScript. Modern web browsers provide a …

Primary Sidebar

More posts from this section

  • How to Get the Last Element of an Array in JavaScript
  • How to use document.write() in JavaScript
  • How to Open a New Tab with JavaScript
  • How to Convert an HTML Node to a String in JavaScript
  • JavaScript String Anchor Method Explained
  • JavaScript String Big Method Explained
  • JavaScript String Blink Method Explained
  • JavaScript String Bold Method Explained
  • JavaScript String charAt Method Explained
  • JavaScript String charCodeAt Method Explained

Copyright © 2025 ActualWizard.com

  • About
  • Terms of Service
  • Privacy Policy
  • Cookie Policy