Highlighting a particular position in a document using javascript code


Copy this code and paste it into the head portion of your html document


<script type="text/javascript">


This function takes gets the url from the link that was clicked,

extracts the anchor (which is also the id of the element on the

page that we want to affect), and changes the class name of that



function highlightTarget(id) {

// What is the url of the clicked link?

var url = id.href;

// Get everything after the '#' in the link--that's our id

var elementId = url.substring(url.lastIndexOf('#') + 1);

// Change the classname of the element of interest:

document.getElementById(elementId).className = 'highlighted';




<style type="text/css">

.highlighted { background-color:#f90; }



Copy the text below into the body of your document


<a href="#tip" onclick="highlightTarget(this)">Mullins Beach</a><br>


<a name="tip">beach</a><br><br>