Solved

Change background color of table cell onclick

Posted on 2011-09-28
8
531 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
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
html input 8 43
Insert Button on a table 16 38
Possible propagation problem 1 25
custom authorization on controller action and HTML in asp.net mvc 1 28
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
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…

829 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