473,411 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

How can I read the current background color

Claus Mygind
571 512MB
I stream out a report with alternating white and light grey background colors in the table elements.

I am using ajax to update the content of a table and I use the response text to adjust the text decoration and the color of the text. But my problem is this. The background color of the element is also changed even though I did not specify that it get changed. (I am using firefox only)

My thought was then, I want to read the background color before I alter the text and then set the background color to that value.

In the example below how can I first determine if the line that is being changed has the white or light grey background so I can use that to reset the background color?

Expand|Select|Wrap|Line Numbers
  1. elem.setAttribute('style','color: red; text-decoration: line-through; background-color: ?????;');
  2.  
Jul 6 '09 #1
8 3023
drhowarddrfine
7,435 Expert 4TB
This is not a HTML/CSS question and should be moved to the Javascript board.
Jul 6 '09 #2
acoder
16,027 Expert Mod 8TB
Moved.

How are you updating the content of the table? It shouldn't change the background colour. Anyway, to get the background colour:
Expand|Select|Wrap|Line Numbers
  1. elem.style.backgroundColor
Jul 6 '09 #3
Claus Mygind
571 512MB
@acoder
The short answer is see: parseResults() function at the bottom of this message. But I have given you the entire flow

1. How I stream out the page from the server

2. How the user can change the status of a contact.

3. my ajax call which goes out to make the change and comes back to update the color of the text of the name of the person's status that was changed.

On my server side scripting I use the variable "wkRowBgColor" to alternate the color of each line like this

Expand|Select|Wrap|Line Numbers
  1.  
  2.          puts([   <td])
  3.          puts([    id="name_]+c["CONTACTKEY"].value+["])
  4.          puts([       nowrap="nowrap"])
  5.          puts([    style="background-color:]+wkRowBgColor+[;"])
  6.          puts([   >]+ cValue +[</td>])
  7.  
  8.  
I do not want to change the line background color, but when I update (change the color of the text) to indicate the status has changed then the background color also changes.

In the column next to the name on the table are 3 radio buttons which when clicked calls the changeStatus() function:
Expand|Select|Wrap|Line Numbers
  1. puts([   <td])
  2. puts([    id="attr_]+c["CONTACTKEY"].value+["])
  3. puts([       nowrap="nowrap"])
  4. puts([    style="background-color:]+wkRowBgColor+[;"])
  5. puts([   >])
  6. puts([Export]+;
  7.     [<input ]+; 
  8.           [type="radio" ]+; 
  9.        [id="exp_]+c["CONTACTKEY"].value+["]+;
  10.           [name="]+c["CONTACTKEY"].value+[" ]+; 
  11.           [value="Save" ]+;
  12.           [onclick="changeStatus(this);"]+;
  13.           [ checked ]+;
  14.         [/> ]+;
  15.         [Remove from list ]+;
  16.         [<input  ]+;
  17.            [type="radio" ]+;
  18.       [id="rem_]+c["CONTACTKEY"].value+["]+;
  19.       [name="]+c["CONTACTKEY"].value+[" ]+;
  20.           [value="Delete" ]+;
  21.           [onclick="changeStatus(this);"]+;
  22.     [/>]+;
  23.     [Delete From Company ]+;
  24.     [<input ]+;
  25.       [type="radio" ]+;
  26.           [id="del_]+c["CONTACTKEY"].value+["]+;
  27.       [name="]+c["CONTACTKEY"].value+[" ]+; 
  28.       [value="RemoveFromCompany" ]+;
  29.           [onclick="changeStatus(this);"]+;
  30.        [/>]+;
  31.    [</td>])
  32.  
Then here is my ajax call that updates the table after the status has changed.
Expand|Select|Wrap|Line Numbers
  1. var xmlHttp;
  2.  
  3. function createXMLHttpRequest() {  
  4.   if (window.ActiveXObject) {
  5.       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  6.   }else if (window.XMLHttpRequest) {
  7.       xmlHttp = new XMLHttpRequest();
  8.   }
  9. }
  10.  
  11. function changeStatus(obj)
  12. {
  13.     var cKey = obj.id.substring(4);
  14.     var cAction = obj.id.substring(0,4);
  15.     var cLocation = document.getElementById("wkWhere").value.substring(3,13);
  16.  
  17.     createXMLHttpRequest();
  18.     var url = "Process.exe?timeStamp=" + new Date().getTime();
  19.     var queryString = createQueryString(cKey, cAction, cLocation) + "&method=POST";
  20.     xmlHttp.onreadystatechange = handleStateChange;
  21.     xmlHttp.open("POST", url, true);
  22.     xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  23.     xmlHttp.send(queryString);
  24. }
  25.  
  26. function createQueryString(cKey, cAction, cLocation) {
  27.     var queryString  = "CONTACTKEY=" + cKey;
  28.         queryString += "&action=" + cAction;
  29.         queryString += "&location=" + cLocation;
  30.     return queryString;
  31. }
  32.  
  33. function handleStateChange() {
  34.    if (xmlHttp.readyState == 4) {
  35.       if (xmlHttp.status == 200) {
  36.          parseResults();
  37.       }
  38.    }
  39. }
  40.  
  41. function parseResults() {
  42.  
  43.    var returnedString = xmlHttp.responseText;
  44.    var aReturn = new Array();
  45.  
  46.    aReturn = returnedString.split(';');
  47.  
  48.   if ( aReturn[0] == "An error occured") { 
  49.      eAlert  = aReturn[0]+"\n"
  50.  
  51.      if ( aReturn[1] == "Invalid client Id") { 
  52.         eAlert += aReturn[1]+" "+document.getElementById("ID").value+"\n"
  53.      }else{ 
  54.         eAlert += aReturn[1]+"\n"
  55.      }
  56.  
  57.      eAlert += aReturn[2]+"\n"
  58.      eAlert += aReturn[3]
  59.      alert( eAlert );
  60.   }else{ 
  61.     var elem = document.getElementById(aReturn[0]) ;
  62.     if (aReturn[1] == 'deleted')
  63.     {
  64.         elem.setAttribute('style','color: red; text-decoration: line-through;');
  65.         document.getElementById(aReturn[2]).innerHTML = "deleted" ;
  66.     }else{
  67.         if (aReturn[1] == 'F')
  68.         {
  69.             elem.setAttribute('style','color: green; text-decoration: line-through; ');
  70.         }else{
  71.             elem.setAttribute('style','color: black; text-decoration: none;');
  72.         }
  73.     }
  74.   }
  75. }
  76.  
As you can see by parseResults() function all I change is the text color, but it seems like the background color of the cell inherits the background color of the form outside the table, even though I did not touch that attribute.

My question really though was - Is it possible with my javaScript to read the current line's background color, save it to a variable, then update the background color after I make the text color change?

The problem is I don't know if the change is occurring to a white or light grey background color.
Jul 9 '09 #4
Claus Mygind
571 512MB
@acoder

I tried your suggestion like this:
Expand|Select|Wrap|Line Numbers
  1. function changeStatus(obj)
  2. {
  3.   var chkColor = obj.style.backgroundColor;
  4.   alert(chkColor);
  5.  
  6.     var cKey = obj.id.substring(4);
  7.     var cAction = obj.id.substring(0,4);
  8.     var cLocation = document.getElementById("wkWhere").value.substring(3,13);
  9.  
  10.  
And it did not return any message in the alert.
Jul 9 '09 #5
acoder
16,027 Expert Mod 8TB
I think the problem is that you're resetting the style when you set the style attribute, so what you should do is just set the properties required:
Expand|Select|Wrap|Line Numbers
  1. elem.style.color="green";
  2. elem.style.textDecoration="line-through";
Jul 9 '09 #6
Claus Mygind
571 512MB
@acoder
Thank you very much. That worked perfectly.


Although I do not need it anymore, I am still wondering can you read the background color of an element?
Jul 9 '09 #7
acoder
16,027 Expert Mod 8TB
Yes, you can. If the background colour has been specified inline:
Expand|Select|Wrap|Line Numbers
  1. <td ... style="background-color:...">
then you can use what I posted earlier, i.e.
Expand|Select|Wrap|Line Numbers
  1. elem.style.backgroundColor
but if it was via a stylesheet, then you'd have to use getComputedStyle (for standards-compliant browsers) and currentStyle for IE.
Jul 9 '09 #8
rnd me
427 Expert 256MB
@Claus Mygind
Expand|Select|Wrap|Line Numbers
  1. function getstyle(obj, cAttribute) {
  2.     if (obj.currentStyle) {
  3.       this.getstyle = function (obj, cAttribute) {return obj.currentStyle[cAttribute];};
  4.     } else {
  5.       this.getstyle = function (obj, cAttribute) {return D.defaultView.getComputedStyle(obj, null)[cAttribute];};
  6.     }
  7.     return getstyle(obj, cAttribute);
  8. }
  9.  
example:
Expand|Select|Wrap|Line Numbers
  1. alert(getstyle(document.body, "color"));
Jul 9 '09 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Dan | last post by:
When a user clicks on a link in my menu, I want the background color of the link that comes up in the hover to remain on the destination page. My menu looks like this: <div id="adminmenu"> <a...
14
by: Don G | last post by:
Within the menu for my site, I have inserted 'class="current"' within the link for the current page. So the link looks somthing link: <li><a href="index.php" class="current">Home</a></li> The...
29
by: pb648174 | last post by:
I have a very long transaction that runs on the same database that other users need to use for existing data. I don't care if they see data from the transaction before it is done and am only using...
16
by: michael | last post by:
Is it possible to get all href URLs contained in a unordered list and place them in an array? Or in fact two different arrays, differently named one for each <ul> group? <ul> <li><a...
2
by: Sandman | last post by:
Just looking for suggestion on how to do this in my Web application. The goal is to keep track of what a user has and hasn't read and present him or her with new material I am currently doing...
2
by: Amit W. | last post by:
Hi I'm trying to use Rockford Lhotka's "Implementing a Background Process in Visual Basic .NET" in C#" ...
3
by: Rhino | last post by:
Yesterday, I reworked the index on my site - http://sfl.london.on.ca - so that they used list markup and I'm quite pleased with them. However, I'm having a problem with one small aspect of the menu...
4
by: DaveR | last post by:
I have a background thread to accept and process TCP/IP connections. When the application shuts down I want to gracefully terminate it. The thread will block in NetworkStream.Read() waiting for...
23
by: Dan Tallent | last post by:
A textbox has a attribute for ReadOnly. This seems like such a simple concept. When a textbox is set to read only the user cannot change the contents of the field. I have been trying to find...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.