Current location: Hot Scripts Forums » General Web Coding » JavaScript » color code issue


color code issue

Reply
  #1 (permalink)  
Old 03-30-10, 06:13 AM
jonnekke jonnekke is offline
Code Guru
 
Join Date: Oct 2005
Location: holland!
Posts: 704
Thanks: 0
Thanked 0 Times in 0 Posts
color code issue

Hi there...

I have a question about getting color with javascript. I got a div with text color set to
Code:
<div id="pagnr" style="color:#5f5347;">123456</div>
When I try to get the color in javascript by using:
Code:
document.getElementById("pagnr").style.color
it returns: rgb(95, 83, 71) instead of #5f5347

Can I change this and how do I do it then..

_j
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 03-30-10, 07:34 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 03-31-10, 11:45 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by jonnekke View Post
Hi there...

I have a question about getting color with javascript. I got a div with text color set to
Code:
<div id="pagnr" style="color:#5f5347;">123456</div>
When I try to get the color in javascript by using:
Code:
document.getElementById("pagnr").style.color
it returns: rgb(95, 83, 71) instead of #5f5347

Can I change this and how do I do it then..

_j
Internet Explorer returns the HEX value of color.
While Mozilla Firefox returns the rgb() value of color.
So you need to check for either condition and convert as needed.

Example:
HTML Code:
<div id="pagnr" style="color:#5f5347;">123456</div>
<script>
function toRGBHex(num)
{
 var decToHex="";
 var prr = new Array();
 var arr = new Array();
 var numStr = new String();
 numStr = num;
 prr = numStr.split("(");
 numStr = prr[1];
 prr = numStr.split(")");
 numStr = prr[0];
 arr = numStr.split(",");
 for(var i=0;i<3;i++)
 {
  var hexArray = new Array( "0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
  var code1 = Math.floor(arr[i] / 16);
  var code2 = arr[i] - code1 * 16;
  decToHex += hexArray[code1];
  decToHex += hexArray[code2];
  }
 return (decToHex);
 }

var c = document.getElementById("pagnr").style.color;
var CheckHEX = c.split("#");
if(CheckHEX[1]){c = CheckHEX[1];}
else{c = toRGBHex(c);}
alert("#" + c);
</script>
__________________
Jerry Broughton

Last edited by job0107; 03-31-10 at 11:49 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] PHP AJAX and IE disabled option issue Dan Man PHP 1 08-26-08 03:34 PM
PHP Mailing Spam issue creativeartist PHP 2 08-14-08 07:58 AM
css issue darkcarnival CSS 4 10-11-05 09:35 PM
Issue Tracking and Task management SlackeR Script Requests 8 10-05-05 05:29 PM
Language issue in from property, Please Help AskAR ASP 0 08-10-05 07:40 AM


All times are GMT -5. The time now is 11:23 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.