Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2006-10-25
5
Medium Priority
?
833 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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

604 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