Solved

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

Posted on 2006-10-25
5
814 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
[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
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

707 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