Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 653
  • Last Modified:

Change background color of table cell onclick

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
peternordberg
Asked:
peternordberg
3 Solutions
 
sonawanekiranCommented:
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
 
Gurvinder Pal SinghCommented:
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
 
ansudhindraCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
peternordbergAuthor Commented:
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
 
Gurvinder Pal SinghCommented:
please share the code that you have tried
0
 
peternordbergAuthor Commented:
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
 
Gurvinder Pal SinghCommented:
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
 
peternordbergAuthor Commented:
It helped me on the right track.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now