Solved

Change <td> attributes via DOM.. How to??

Posted on 2006-10-25
5
799 Views
Last Modified: 2010-05-18
I'm trying to created a click tab effect but need help. As it is I have two table cells with a class and onclick as so:

<td class="selectedtab" onclick="switchtab(0)" tab="tab1"><td class="tab" onclick="switchtab(1)" id="tab2">

What I'm trying to do is create a switchtab function that will change to class and onclick to opposites, like this:

<td class="tab" onclick="switchtab(1)" tab="tab1"><td class="selectedtab" onclick="switchtab(0)" id="tab2">

All I have is this:
function switchtab(select) {
     getElementByID(tab1)
              ...
     getElementByID(tab2)
              ...
}

How do I switch class and onclick attirbutes like this. Pls help. Thanks in advance for all the help.
0
Comment
Question by:bemara57
  • 3
5 Comments
 
LVL 41

Accepted Solution

by:
HonorGod earned 500 total points
ID: 17805543
I'm sorry, but I don't quite understand what it is you are trying to do.  Di you mean the "before" to look like:

<td class="selectedtab" onclick="switchtab(0)" id="tab1"><td class="tab" onclick="switchtab(1)" id="tab2">

 and the after to look like:

<td class="tab" onclick="switchtab(1)" tab="tab1"><td class="selectedtab" onclick="switchtab(0)" id="tab2">
???

  Might this be what you want?

<html>
<head>
<title>Q_22037436.html</title>
<script type='text/javascript'>
// See: http://www.experts-exchange.com/Web/Web_Languages/JavaScript/Q_22037436.html
  function switchTd( id1, id2 ) {
    var td1 = document.getElementById( id1 );
    var td2 = document.getElementById( id2 );
    var temp = td1.className;
    td1.className = td2.className;
    td2.className = temp;
  }
</script>
</head>
<body>
<table border='1' width='50%'>
  <tr>
    <td id='tab1' class='selectedtab' onclick='switchTd("tab1","tab2")'>Cell 1</td>
    <td id='tab2' class='tab'         onclick='switchTd("tab1","tab2")'>Cell 2</td>
  </tr>
</table>
</body>
</html>


0
 
LVL 41

Expert Comment

by:HonorGod
ID: 17805553
Doing it this way, you wouldn't need to change the onclick attributes
0
 
LVL 5

Expert Comment

by:daohailam
ID: 17805564
<script>
var sel = null;
function select(tab) {
      if (sel != null)
            sel.className = 'tab';
      tab.className = 'selectedtab';
      sel = tab;
}
</script>
...
<table>
<tr>
<td class="tab" onclick="select(this)">Tab1</td>
<td class="tab" onclick="select(this)">Tab2</td>
<td class="tab" onclick="select(this)">Tab3</td>
<td class="tab" onclick="select(this)">Tab4</td>
</tr>
<tr>
<td>V1</td><td>V2</td><td>V3</td><td>V4</td>
</tr>
</table>
0
 

Author Comment

by:bemara57
ID: 17805596
PERFECT!! Thanks again HonorGod.
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 17805738
No problem.  Thanks for the 'A'
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now