Solved

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

Posted on 2009-07-02
5
291 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:rcx_support
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 19

Assisted Solution

by:Albert Van Halen
Albert Van Halen earned 20 total points
ID: 24761574
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
 

Author Comment

by:rcx_support
ID: 24761669
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
 

Author Comment

by:rcx_support
ID: 24761946
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
 

Accepted Solution

by:
rcx_support earned 0 total points
ID: 24769784
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
 
LVL 19

Expert Comment

by:Albert Van Halen
ID: 24770207
Cool that you found out.
I keep the setActive in mind myself...
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Rotate images for Web Page 11 28
Why is my $_POST not going to results page 10 41
Slush on text 2 12
Web page design problem 3 18
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question