473,378 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

OnClick change element css class

76
Okay I've been wracking my brain all morning trying to figure this one out. It seems like to me it -should- work so kudos for anyone who can tell me what I'm doing wrong.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <style type="text/css">
  4. .subset A{
  5. background-color:#EAEFF5;
  6. color:#000000;
  7. height:12px;
  8. padding:2px;
  9. font-weight:bold;
  10. border:1px solid black;
  11. font-family:Tahoma;
  12. font-size:medium;
  13. margin-left:5px;
  14. font-size:10px;
  15. text-decoration:none;
  16. }
  17. .subset A:HOVER{
  18.   background-color:#FFFFFF;
  19. }
  20. .subsetAlt A{
  21. background-color:#336699;
  22. color:#FFFFFF;
  23. height:12px;
  24. padding:2px;
  25. font-weight:bold;
  26. font-family:Tahoma;
  27. font-size:medium;
  28. border:1px solid black;
  29. margin-left:5px;
  30. font-size:10px;
  31. text-decoration:none;
  32. }
  33. .subsetAlt A:HOVER{
  34.   background-color:#000000;
  35. }
  36. </style>
  37. <script language="javascript" type="text/javascript">
  38.  function setSubSection(myField,newFieldExt,totalField)
  39. {
  40.   var fieldNumber = myField.substr(0,1);
  41.   var sectionField;
  42.   var btnField;
  43.   var fieldTotals = totalField;
  44.  
  45.   for (var x=1;x<=fieldTotals;x++)
  46.   {
  47.     sectionField = x + newFieldExt;
  48.     btnField = document.getElementById(sectionField + "btn");
  49.     if(x==fieldNumber)
  50.     {
  51.     document.getElementById(sectionField).style.display = "block";
  52.     btnField.className = btnField.className + "Alt";
  53.     }
  54.     else
  55.     {
  56.     document.getElementById(sectionField).style.display = "none";
  57.     btnField.className = btnField.className.replace(/Alt/g,'';
  58.     }
  59.   }
  60. }
  61. function changeClass(fieldName,classType)
  62. {
  63.   var myField = document.getElementById(fieldName);
  64.   var myClass = myField.className;
  65.   if (classType == 1)
  66.   {
  67.      myClass = myClass + "Alt";
  68.   }
  69.   else
  70.   {
  71.      myClass = myClass.replace(/Alt/g,'');
  72.   }
  73.   myField.className = myClass;
  74. }
  75. </script>
  76. </head>
  77. <body>
  78. <div id="peopleSet">
  79.   <span id="1_personbtn" class="subset"><a 
  80.  
  81. href="javascript:setSubSection('1_personbtn','_person','2')">Person 1</a></span>
  82.   <span id="2_personbtn" class="subset"><a 
  83.  
  84. href="javascript:setSubSection('2_personbtn','_person','2')">Person 2</a></span>
  85. </div>
  86. <div id="1_person" style="display:none;border:1px solid black;">This is person 1 
  87.  
  88. information</div>
  89. <div id="2_person" style="display:none;border:1px solid black;">Person 2 is full of 
  90.  
  91. information</div>
  92. </body>
  93. </html>
  94.  
Jul 20 '06 #1
2 100377
Arielle
76
Nevermind I figured out my error.

I needed to make var myClass = myField.className; into var myClass = myField.className.replace(/Alt/g,""); because what was happening is that once declared an alt it didn't know what to do with it the second time around.
Jul 20 '06 #2
An alternate way is to use e.setAttribute("className", "you new class name here"). Example:
http://wiki.foochal.org/index.php/Javascript_CSS
Feb 22 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: EviL KerneL | last post by:
Hi - I am working on a survey project which has a next button that takes you to the next set of questions and so on. It does this by means of an OnClick event which takes the answers from the...
8
by: cool2005 | last post by:
I tried to dynamically add/clone a <tr> from an existing <tr> to a <table> but the problem is that I need to have a "onclick" event handler in the <tr>. I have tried following A. approach...
53
by: usenet | last post by:
See <ul> <li><a name="link1" onClick="alert(this.name);return false;" href="#">Link1</a></li> <li><a name="link2" href="javascript:alert(this);">Link2</a></li> <li>Item 3</li> </ul> ...
0
by: Diane Yocom | last post by:
I'm very new to ASP.Net and probably jumped in a little over my head, but... I'm trying to create a user control that will control navigation through my site. It's sortof like Amazon.com, where...
5
by: moondaddy | last post by:
I have a <a> element in a datagrid which wraps some asp.net labels. this element also has an onclick event which does not fire in netscape 6 (and perhaps other browsers for all I know...). Below...
7
by: Giacomo | last post by:
I work on a page structured like: <h2> ... </h2> <div ="div1" class="show"> ... </div> <h2> ... </h2> <div id="div2" class="show"> ... </div> <h2> ... </h2> <div id="div3" class="show">...
3
by: Reinhold Schrecker | last post by:
Hi there, I am trying to generate a dynamic menu with JavaScript/DOM and have problems to set the onclick-attribute for my <a>-elements. The following code works fine in Opera and Mozilla but...
1
by: garey | last post by:
Hello - I have written a small table of contents manager. If an entry has sub-entries, it has a plus in front of it. If the user clicks on the plus, the sub-entries are displayed. The table of...
1
by: nathank | last post by:
This seems trivial, but I have yet to find a way to move "onclick" out of the element itself and up to the <head> or to a .js file. I'd like to clear up that clutter and just have the li element and...
5
by: SM | last post by:
Hello, I have an <ul>, and when i click on a item i want to add a class to that item. The class itself will change some display properties, using CSS. See code below. But, whenever i click on a...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.