Solved

Change background color of table cell onclick

Posted on 2011-09-28
8
493 Views
Last Modified: 2012-08-13
Hi,

I have this scenario where I would like to change the background color on the table cell that the user clicks on and when the user then clicks another cell the one first clicked on should go back to the normal/original background color.

I tried this but didn't get it to work.
 var whosChanged = null;
        function changeMe(el) {
           
            el.style.backgroundColor = "#FE3000";
            el.style.color = "#000000";
            if (whosChanged != null) {
                alert("hej");
                whosChanged.style.backgroundColor = ""
                whosChanged.style.color = ""
            }
            
            whosChanged = el;
        }

Open in new window


and then...

onclick="changeMe(this);"

Thanks for help!

Peter
0
Comment
Question by:peternordberg
8 Comments
 
LVL 17

Accepted Solution

by:
sonawanekiran earned 167 total points
Comment Utility
Check below code

Demo:
http://jsfiddle.net/QSU3j/
<html>
<head>
<title>Kiran Test</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
     $("table td").css("background-color","red");
     $("table td").click(function(){
       $("table td").css("background-color","red");
       $(this).css("background-color","blue");
     });
});
</script>
</head>
<body>
<table width="200" border="1" cellspacing="5" cellpadding="5">
    <tr>
        <td>cell1
        </td>
        <td>cell2
        </td>
        <td>cell3
        </td>
      
    </tr>
      <tr>
        <td>cell11
        </td>
        <td>cell21
        </td>
        <td>cell31
        </td>
      
    </tr>
   
</table>

</body>
</html>

Open in new window

0
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 166 total points
Comment Utility
class2 should be the default class
<style>
  td.class1
  {
      background-color : #FE3000;
      color:#000;
  }
  td.class2
  {
      background : none;
      color:inherit;
  }
</style>

function changeMe(el)
{          
    el.classname = el.classname == "class1" ?  "class2":"class1";
}
0
 
LVL 13

Assisted Solution

by:ansudhindra
ansudhindra earned 167 total points
Comment Utility
check this one...
http://jsfiddle.net/Sudhindra/XXAGY/
Code is
 
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title> - jsFiddle demo</title>
  <script type='text/javascript' src='http://code.jquery.com/jquery-1.6.3.js'></script>
  
  <link rel="stylesheet" type="text/css" href="/css/normalize.css">
  <link rel="stylesheet" type="text/css" href="/css/result-light.css">
  
  <style type='text/css'>
    table {
    width:200px;
    cursor:pointer;
}
.normalColor {
    background-color:blue;
}

.clickColor {
    background-color:red;
}

  </style>
  
  <script type='text/javascript'>
  //<![CDATA[ 
  $(window).load(function(){
  $('.normalColor').click(function(){
    $('.clickColor').attr('class','normalColor');        
    $(this).attr('class','clickColor');
});
  });
  //]]> 
  </script>
  
</head>
<body>
  
<table id="colorTab">
    <tr class="normalColor"><td>1</td><td>One</td></tr>
    <tr class="normalColor"><td>2</td><td>Two</td></tr>
    <tr class="normalColor"><td>3</td><td>Three</td></tr>
    <tr class="normalColor"><td>4</td><td>Four</td></tr>
</table>
  
</body>


</html>

Open in new window

0
 

Author Comment

by:peternordberg
Comment Utility
Hi and thanks for your answers,

I would rather have it in a function that I can call at the Onclick event since I also need to retrieve a value from the cell I'm clicking on. I tried your code gurvinder372, but I couldn't get it to work.

Thanks for your help!

Peter
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
please share the code that you have tried
0
 

Author Comment

by:peternordberg
Comment Utility
This code:

 function changeMe(el,id) {
        
            var scheduleID = document.getElementById("<%=scheduleID.ClientID %>");
            scheduleID.value = id;
            alert(scheduleID.value)
            el.classname = el.classname == "class1" ? "class2" : "class1";
        }

Open in new window


And the td tag like this (it's done in code behind in asp.net):
s.AppendLine("<td width=""100px"" style="" height:50px; color:#fff; border:1px solid #c9c9c9; background-color:#fff; padding:0px; cursor:pointer;"" onMouseOver=""this.style.background='lightyellow'"" OnMouseOut=""this.style.background='white'"" OnClick=""this.style.background='lightyellow'; OpenWindow('editSchedule.aspx?subjectID=" & dr("subjectID") & "&scheduleID=" & dr("scheduleID") & "', 'RadWindow1');""><img src=""../../Images/Grafik/bgCell.png"" width=""100"" /><div style=""padding:5px;"">" & dr("subject") & "</div></td>")

Open in new window


The alert triggers allright but the background color isn't changing.

Peter
0
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
first of all, you need to use class than inline styles, so that its easier to change the class only

make line 6 as

el.classname = (el.classname == "class1") ? "class2" : "class1";
0
 

Author Closing Comment

by:peternordberg
Comment Utility
It helped me on the right track.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

743 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

15 Experts available now in Live!

Get 1:1 Help Now