Skip to content

ProtoString toAnchor() Method

Seth Clydesdale edited this page Jun 1, 2015 · 4 revisions

<< Back to DOM Node Methods

Examples

Converting a string into an anchor point.

'Anchor Point'.toAnchor(); // returns a node : <a href="#Anchor-Point" id="Anchor-Point">Anchor Point</a>

Specifying the return type and custom identifier.

'Brewing Coffee'.toAnchor('string', 'chapter-2'); // returns a string : '<a href="#chapter-2" id="chapter-2">Brewing Coffee</a>'

Description

The toAnchor method converts a text string into an anchor point. The data returned by default is a DOM Node, however if specified, a String can be returned instead. The String this method was executed on acts as both the ID and Hash if no custom ID is specified.

  • This method makes use of cleanId(), to convert text into a valid identifier, only if an ID is not specified.
  • This method can also be considered a sort of polyfiller for the native anchor() method.

Syntax

String.toAnchor(type, id);

Parameters

Parameter Description
type A string specifying the data type to be returned. Possible values are node and string. Defaults to node if left undefined.
id A string specifying a custom identifier for the anchor. Defaults to using the string this method was executed on if left undefined.
Clone this wiki locally