473,401 Members | 2,125 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,401 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 100380
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.