I want to be able to remove a class from x amount of elements. The elements that I am currently using is a tags. There can be unlimited amount of a tags on the document at any given time because in a different div an user can create links. This prevents me from using specific ids. But I want to only have one tag highlighted at any given time. However, I have only seen how to do it with JQuery, which I can't use. I saw a different example using Javascript, but that was for a fixed size array.
This is the code for my highlighting class:
<DOCTYPE html>
<html>
<head>
<style>
.selected{border:dotted 2px;border-color:gray;}
</style>
</head>
<body>
<div id='linkEditDiv'>
<form name='linkEditor'>
<table>
<input type="hidden" name='linkId'>
<tr><th>Label:</th><td> <input type='text' name='label'></td></tr>
<tr><th>Link:</th><td> <input type='text' name='link'></td></tr>
<tr><th></th><td><input type="button" onclick='submitForm()' value="OK" ><input type="button" onclick='cancel()' value="Cancel" ></td></tr>
</table>
</form>
</div>
<a href='#' onclick='edit("lr1")' id='lr1'>link1</a>
<a href='#' onclick='edit("lr2")' id='lr2'>link1</a>
<a href='#' onclick='edit("lr3")' id='lr3'>link1</a>
</body>
</html>
This is the JavaScript that adds the highlighting:
function edit(link)
{
var elems = document.getElementsByClassName('selected');
for (var i = 0; i < elems.length; i++) {
elems[i].className = elems[i].className.replace('selected', '')
}
document.getElementById(link).className += "selected"
}
This is the JavaScript that I tried:
var id=document.forms['linkEditor'].elements['linkId'].value;
document.getElementById(id).className = document.getElementById(id).className.replace( /(?:^|\s)selected(?!\S)/g , '' );