473,386 Members | 1,817 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,386 software developers and data experts.

Change text colour in one function

118 100+
So far I have

Expand|Select|Wrap|Line Numbers
  1. function color()
  2. {
  3. var box = document.getElementById('message');
  4.  
  5. box = box.style.color = "#FF0000";    
  6.  
  7. }
  8.  
So with that when you press the link, the text in the the textbox turns red.

I don't want to have to repeat a lot of code over and over for different colours. Is there anyway I can use one function to change to different colours?

So if the onclick is color(red) it turns red or color(blue) turns blue etc...

Would that be possible and what code would I need?

Hope I was clear,

Thanks,
Sam
Oct 7 '07 #1
4 1321
Logician
210 100+
Expand|Select|Wrap|Line Numbers
  1. box = box.style.color = "#FF0000";    
  2.  
  3.  
You don't need box= there.


So if the onclick is color(red) it turns red or color(blue) turns blue etc...
That would have to be color("red")

An associative array is one way to translate from a string. For simplicity there's no error checking here:
Expand|Select|Wrap|Line Numbers
  1. function color(elemId, clr)
  2. {
  3.  var table=[];
  4.  table['red']='#FF0000'; table['blue']='#00FF00'; table['green']='#00FF00' ;
  5.  
  6.  document.getElementById(elemId).style.color=table[clr];
  7. }
  8.  
onclick="color('IdOfMyElement', 'red')"
Oct 8 '07 #2
gits
5,390 Expert Mod 4TB
...
Expand|Select|Wrap|Line Numbers
  1. var table=[];
  2. table['red']='#FF0000'; table['blue']='#00FF00'; table['green']='#00FF00' ;
  3.  
...
hi ...

i think i have to mention some things here. the code above misuses an array as an object ... try:

Expand|Select|Wrap|Line Numbers
  1. alert(table.length);
you will get 0. what you do with the above assignments is to create member-variables within the array-object itself, you could also do:

Expand|Select|Wrap|Line Numbers
  1. table.red = 'foo';
in case you want to loop now ... you MUST use:

Expand|Select|Wrap|Line Numbers
  1. for (var i in table) {
  2.     alert(table[i]);
  3. }
since you use now the array-object! this is quite a misuse of it. try to push an element and now our 'normal' array will contain 1 element.

the correct way would be to use an object at all.

Expand|Select|Wrap|Line Numbers
  1. var table = {};
  2.  
and use that as a 'quasi'-assoc-array ... but it is an object :) ... in javascript we have no assoc-arrays ... but we may use objects for that.

kind regards
Oct 8 '07 #3
Logician
210 100+
hi ...

i think i have to mention some things here. the code above misuses an array as an object ... try:

Expand|Select|Wrap|Line Numbers
  1. alert(table.length);
you will get 0. what you do with the above assignments is to create member-variables within the array-object itself, you could also do:

Expand|Select|Wrap|Line Numbers
  1. table.red = 'foo';
in case you want to loop now ... you MUST use:

Expand|Select|Wrap|Line Numbers
  1. for (var i in table) {
  2.     alert(table[i]);
  3. }
since you use now the array-object! this is quite a misuse of it. try to push an element and now our 'normal' array will contain 1 element.

the correct way would be to use an object at all.

Expand|Select|Wrap|Line Numbers
  1. var table = {};
  2.  
and use that as a 'quasi'-assoc-array ... but it is an object :) ... in javascript we have no assoc-arrays ... but we may use objects for that.

kind regards
I don't agree that there is any abuse going on otherwise the syntax would not be allowed. I know that elements created using associative syntax are stored differently, which is why I didn't use a loop or try to read a .length property. The purpose was to let the interpreter effectively do the looping, and I could just as easily have written:
Expand|Select|Wrap|Line Numbers
  1. var table={red:'#FF0000', green:'#00FF00', blue:'#0000FF'};
which works equally well with the following statement and I'm sure is treated the same way internally.
Oct 8 '07 #4
gits
5,390 Expert Mod 4TB
Expand|Select|Wrap|Line Numbers
  1. var table={red:'#FF0000', green:'#00FF00', blue:'#0000FF'};
this is correct usage, because you create a regular javascript object with its literals ... but in the above post you did use an Array ... you constructed it literally with [] ! and then used its constructing object to store properties in it ... this is definitly a misuse of the Array-Object!

Expand|Select|Wrap|Line Numbers
  1. // this is an Array ... you can use all array-methods like
  2. // push, pop, etc. with it and it CANNOT be assoc
  3. var a = []; 
  4.  
  5. // this is an object ... that you may use AS an assoc array
  6. // but it is an object at all
  7. var o = {};
  8.  
kind regards

ps: its not an abuse since an Array is an Object too. try to alert(typeof []); so its not a syntactically abuse ... its a semantic one ...
Oct 8 '07 #5

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

Similar topics

4
by: Bill Davy | last post by:
To make life easier for my users, I'd like to colour my prompt string (as handed to raw_input()) a different colour to that produced by print. I'm using Python 2.4.1 and IDLE 1.1.1 on Windows XP. ...
6
by: Louise | last post by:
Hi I have written an HTML pages which does not have any colour specifying tags as far I know. When I view this in an Microsoft internet explorer browser it appears with a white background and...
7
by: kroger | last post by:
Hi, one part of my website is at: http://www.psych.nmsu.edu/~jkroger/lab/undergrads.html I want to make the date at the top right darker blue. But when I do that, all the light blue text...
3
by: Peter Williams | last post by:
Hi All, I want to write some javascript for a html page which does the following. Imagine that the page contains a table with 2 columns and 3 rows, e.g.: +---+---+ | A | B | +---+---+
4
by: Dwight | last post by:
I need to change the colour of text values within a table. For example "Software" being red, "Lab Software" blue etc. To change colour of number values simply, 1, entered into the format section...
4
by: gurdz | last post by:
Does anyone know how to change the colour of the text in the console in C??
3
by: Bundy | last post by:
Hi How do I change the background colour of an input box in a form as soon as the value is changed? Also the background should revert back to it's original colour if the user decides that they...
0
by: Badino | last post by:
Hi, Can someone tell me what to put in this code so that if a user selects 0 (Black) then make the font white (0) as my default colour is black on my Excel spreadsheet. Private Type...
4
by: html | last post by:
Hello all, I need to change the colour of my text. For the paragraph I do document.fgColor ="blue" ; but how do I do this for the select tags? ....and I prefer it to apply to all select tags...
4
by: sgxbytes | last post by:
Hi, My html has <Table style="year-button-archive" width="60%" > <tr> <td class="gold" > <a title="year">By Year:</a> <td class="gold"> <a id = "2008"...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.