Solved

Change background color of table cell onclick

Posted on 2011-09-28
8
538 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
[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
8 Comments
 
LVL 17

Accepted Solution

by:
sonawanekiran earned 167 total points
ID: 36716622
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
ID: 36716624
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
ID: 36716633
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:peternordberg
ID: 36717234
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
 
LVL 40

Expert Comment

by:gurvinder372
ID: 36812696
please share the code that you have tried
0
 

Author Comment

by:peternordberg
ID: 36813125
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
ID: 36813858
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
ID: 36966638
It helped me on the right track.
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…

726 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