Sriram Sriram - 1 year ago 75
HTML Question

How to hyperlink the character in paragraph which coming in Json data

Problem Statement :

I am getting a

data with Paragraph and LinkLocation(location for hiperlink the characters in the paragraph using LinkLocation:(6,12)). It means, character 6 to character 12 in the paragraph we need to hyperlink and redirect that to another page. Please advice me how to do that.

link:{id: 3, index: "Law 1.1", name: "Number of Players ",…}
content:"Law 1.1 shall be replaced by the following."
description:"Law 1.1 shall be replaced by the following:"
index:"Law 1.1"
name:"Number of Players "
linkposition:"1, 6"

This is my JSON data. from this data i want to hyperlink the "content" paragraph as per the "Linkposition". for example here the link position is "1,6". so i want to hyperlink the "Law 1.1" in the content Paragraph.

Answer Source

If you want to hyperlink a subset of characters in a string you can do it this way:

var data = {
  paragraph: "hello world",
  link: '1,5'

var limits = link.split(',');

//subtract one cos substring method is inclusive of bottom limit and exclusive of upper limit
var lowerLimit = limits[0]-1;
var upperLimit = limits[1];

var newParagraph = data.paragraph.substr(0, lowerLimit) + "<a href='otherpage.html'>" + data.paragraph.substr(lowerLimit, upperLimit) + "</a>" + data.paragraph.substr(upperLimit, data.parapgraph.length);

So that would produce the result

<a href='otherpage.html'>Hello</a> world

Hence 'Hello' is hyperlinked

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download