How do I set pseudo class of an anchor tag as active through javascript.

I have declared following style in my jsp.

<style type="text/css">
      a.anchorCSS:link, a.anchorCSS:visited {
        display: inline;
        width: 2em;  
        padding: 0.2em;
        line-height: 1.4;
        background-color: #ff0000;
        border: 1px solid white;
        color: #ffffff;
        text-decoration: none;
        text-align: center;
        font-family: Arial, Helvetica, sans-serif;
      font-size: 9pt;
      font-weight: bold;
}

a.anchorCSS:hover {
       background-color: #1998F9;
       color: #fff;
       font-family: Arial, Helvetica, sans-serif;
      font-size: 9pt;
      font-weight: bold;
}

a.anchorCSS:active {
      background-color: #035C9C;
       color: #ffff;
       font-family: Arial, Helvetica, sans-serif;
      font-size: 9pt;
      font-weight: bold;
}
     
      </style>

I am implementing pagination wherein page numbers are displayed as links with above CSS.
But after page numbers are displayed I want to mark the first page as active by default. In subsequent clicks the corresponding page numbers should be active.

I tried the following but its not working.

document.getElementById("1").className='anchorCSS:active';
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<style type="text/css">
	a.anchorCSS:link, a.anchorCSS:visited {
 	 display: inline;
 	 width: 2em;  
 	 padding: 0.2em;
  	line-height: 1.4;
  	background-color: #ff0000;
  	border: 1px solid white;
  	color: #ffffff;
  	text-decoration: none;
 	 text-align: center;
  	font-family: Arial, Helvetica, sans-serif;
	font-size: 9pt;
	font-weight: bold;
}
 
a.anchorCSS:hover {
 	background-color: #1998F9;
 	color: #fff;
 	font-family: Arial, Helvetica, sans-serif;
	font-size: 9pt;
	font-weight: bold;
}
 
a.anchorCSS:active {
	background-color: #035C9C;
 	color: #ffff;
 	font-family: Arial, Helvetica, sans-serif;
	font-size: 9pt;
	font-weight: bold;
}
	
	</style>
  </head>
  
  
 
  
  <script type="text/javascript">
function performPostSelection(totalRecords,pageSize,currentPage,pageAction){
 
	
	var pattern = /\d/g;
	
	if(pageAction=='F'){
		pagination(totalRecords,pageSize,currentPage);
		
			
	}
	if(pageAction=='P'){
		pagination(totalRecords,pageSize,currentPage);
		
	}
	if(pattern.test(pageAction)){
		
				
	}
	if(pageAction=='N'){
		pagination(totalRecords,pageSize,currentPage);
		
	}
	if(pageAction=='L'){
		pagination(totalRecords,pageSize,currentPage);
		
	}
}
 
function pagination(totalRecords,pageSize,currentPage){
  	
  	var windowSize = 5;
  	
  	var pageNode = document.getElementById('pageSequence');
  	var childs = pageNode.childNodes;
  	 	
  	// alert('numberOfChilds:'+childs.length);
  	/*
  	for(var j=0;j<childs.length;j++){
  		alert('Removing'+childs[j]);
  		alert('j:'+j);
  		pageNode=document.getElementById('pageSequence');
  		childs = pageNode.childNodes;
 		pageNode.removeChild(childs[j]); */
 		
 		while(childs.length >0){
 			// alert('Removing'+childs[0]);
 			// pageNode = document.getElementById('pageSequence');
 			pageNode.removeChild(childs[0]);
 			pageNode = document.getElementById('pageSequence');
 			childs = pageNode.childNodes;
 		}
 	
  	
  	// var pageNode = document.getElementById('pageSequence');
  	
  	var numberOfPages = Math.ceil(totalRecords/pageSize);
  	
  	
   	// alert('NumberOfPage:'+numberOfPages);
  	if(totalRecords > 0){
  		if(currentPage/windowSize >= 2){
  			var link = document.createElement('a');
  			link.setAttribute('href','javascript:performPostSelection('+(totalRecords)+','+(pageSize)+','+(1)+',\'F\')');
  			link.setAttribute('className','anchorCSS');
  			link.setAttribute('style','font-family: Arial, Helvetica, sans-serif;font-size: 8pt;font-weight: bold;');
  			link.innerHTML=' << ';
  			
  			
  			pageNode.appendChild(link);
  		}
  		if(currentPage/windowSize >= 1){
  			var previousPage=(windowSize*((Math.floor(currentPage/windowSize))-1))+1;
  			// alert('PreviousPage:'+previousPage);
  			var link = document.createElement('a');
  			link.setAttribute('href','javascript:performPostSelection('+(totalRecords)+','+(pageSize)+','+(previousPage)+',\'P\')');
  			link.setAttribute('className','anchorCSS');
  			link.setAttribute('style','font-family: Arial, Helvetica, sans-serif;font-size: 8pt;font-weight: bold;');
  			link.innerHTML=' < ';
  			pageNode.appendChild(link);
  		}
  		for(var i=currentPage;i<=numberOfPages;i++){
  			if(i==(currentPage+windowSize)){
  				break;
  			}
  			var link = document.createElement('a');
  			link.setAttribute('href','javascript:performPostSelection('+(totalRecords)+','+(pageSize)+','+i+','+i+')');
  			link.setAttribute('className','anchorCSS');
  			link.setAttribute('style','font-family: Arial, Helvetica, sans-serif;font-size: 8pt;font-weight: bold;');
  			link.innerHTML=i;
  			link.setAttribute('id',i);
  			pageNode.appendChild(link);
  			
  		} // end of for
  		
  		if((numberOfPages)/(currentPage+windowSize) >= 1){
  			var correction = 1;
  			if((currentPage%windowSize)==0){
  				correction=0;
  			}
  			var nextPage=(windowSize*((Math.floor(currentPage/windowSize))+correction))+1;
  			// alert('NextPage'+nextPage);
  			var link = document.createElement('a');
  			link.setAttribute('href','javascript:performPostSelection('+(totalRecords)+','+(pageSize)+','+(nextPage)+',\'N\')');
  			link.setAttribute('className','anchorCSS');
  			link.innerHTML=' > ';
  			pageNode.appendChild(link);
  		}
  		if((numberOfPages)/(currentPage+windowSize)+1 > 2){
  			var correction = 0;
  			if(((numberOfPages)%windowSize)==0){
  				alert(currentPage);
  				correction=1;
  			}
  			var link = document.createElement('a');
  			var firstWindowRec=currentPage%windowSize;
  			var multiplier=numberOfPages/(currentPage-firstWindowRec);
  			// alert('NumberOfPages:'+numberOfPages);
  			// alert('windowSize:'+windowSize);
  			// var lastWindow=(windowSize*(Math.floor(numberOfPages/windowSize)))+1;
  			var lastWindow=(windowSize*((Math.floor(numberOfPages/windowSize))-correction)+1);
  			// alert('lastWindow:'+lastWindow);
  			link.setAttribute('href','javascript:javascript:performPostSelection('+(totalRecords)+','+(pageSize)+','+(lastWindow)+',\'L\')');
  			link.setAttribute('className','anchorCSS');
  			link.setAttribute('style','font-family: Arial, Helvetica, sans-serif;font-size: 8pt;font-weight: bold;');
  			
  			
  			link.innerHTML=' >> ';
  			pageNode.appendChild(link);
  		}
  	}
  	document.getElementById("3").className='anchorCSS';
  } // end of function
</script>
  
  <body>
  	
    This is my JSP page3.
    <input type = 'button' value='SUBMIT' onclick='pagination(401,20,1)'>
   <!--  textarea scrolling="no" rows="10" cols="10" style="border-width:1px; whiteSpace:pre;">fhidoghoashgioadhgghghfgha
    ghadf;soghasdfioghioafgh</textarea> -->
    <form>
    
    <div id="x" name=x>
    <!--  label id="y" name="y"  onclick='pagination(344,20,1)'/>Check -->
    </div>
    <table border="1px">
      
    <tr>
    <td width="400px" align="center">
    	<div id="pageSequence"></div>
    	</td>
    </tr>
    </table>
    
   
    </form>
  </body>
</html>

Open in new window

rcx_supportAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Albert Van HalenAnalyst developerCommented:
You could modify your css 'a.anchorCSS:active' rule to a grouping rule having a.anchorCSS-active and apply that classname to the link
a.anchorCSS:active, a.anchorCSS-active {
    background-color: #035C9C;
    color: #ffff;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 9pt;
    font-weight: bold;
}
 
document.getElementById("1").className='anchorCSS-active';

Open in new window

0
rcx_supportAuthor Commented:
I did the same as suggested by you. Thanks for the same.

But as you know I am implementing pagination.
On clicking search, link to first page should be marked active.
But when 2nd link is clicked, then link 2 should be active and 1st link should again fall back to 'link'.
0
rcx_supportAuthor Commented:
Basically what I want is to change the state of anchor elements from link to active and CSS should get automatically selected as per my CSS declaration .

Something like this--

document.getelementByID("").style.backgroung="blue"; as for changing bg color.

document.getelementByID(").style.class.active='true';  // this behaviour I am loooking for.
0
rcx_supportAuthor Commented:
Thanks Albert for your assistance. I figured out the solution.
I wanted to mark the first anchor tag as active manually  .

This is achieved by setActive() method.

The solution is --

document.getElementById("1").setActive();

 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Albert Van HalenAnalyst developerCommented:
Cool that you found out.
I keep the setActive in mind myself...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.