Solved

Change background color of table cell onclick

Posted on 2011-09-28
8
505 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET 5 Templates 2 66
Materializecss - Different Behaviors in Browser's 1 20
How can i get the length? 7 7
how can i select 3 0
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…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

910 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

20 Experts available now in Live!

Get 1:1 Help Now