tags:

views:

84

answers:

2

Hi everyone. I'm new to JavaScript. I've developed a page using JavaScript in such a way that when I select a color it is applied to the whole page as a background.

I want to develop a page where I can change only the text color. It should get changed (from red to green or something like that), but the page shouldn't get refreshed, and only the selected contents or text color should be changed.

Can any one please help me in this. Any ideas of how to develop it? Thanks in advance.

+1  A: 

Do something like this:

<script>
function changeColor(id)
{
  document.getElementById(id).style.color = "#ff0000"; // forecolor
  document.getElementById(id).style.backgroundColor = "#ff0000"; // backcolor
}
</script>

<div id="myid">Hello There !!</div>

<a href="#" onclick="changeColor('myid'); return false;">Change Color</a>
Sarfraz
-1 For inefficient use of `document.getElementById(id)` and problematic event registration
Justin Johnson
@Justin: Great it would be if you could explain what is inefficient there? It would me great if i could learn. Any links you can provide please?
Sarfraz
It's best to stay away from walking the DOM as much as possible, so you'll want to make as few calls as possible to methods like `document.getElementById`. As for the event registration, it's generally considered bad form to mix presentation and logic. More can be read about it here: http://www.quirksmode.org/js/events_early.html
Justin Johnson
@Justin: I belive if i were you i would not have voted down, i would have simply left a comment to let you know that this way is inefficient. It is not an error mind you please. thanks
Sarfraz
The two main points of your code (event registration and DOM manipulation) are both done in ways that are considered bad practices. Yes, it works functionally, but no, it is not a good recommendation to give to beginners as it starts them down the wrong path of thinking.
Justin Johnson
to change the font color on mouse over can i write the script like dis.. ?? any sugessions pls.. <html> <body> <div id=colorme> <a onMouseover="document.getElementById('colorme').style.color='green'"> green</a> ---- <a onMouseover="document.getElementById('colorme').style.color='red'"> red</a> </div> </body> </html>
Patel
yes that may work :)
Sarfraz
+1  A: 

A rewrite of the answer by Sarfraz would be something like this, I think:

<script>

    document.getElementById('change').onclick = changeColor;   

    function changeColor() {
        document.body.style.color = "purple";
        return false;
    }   

</script>

You'd either have to put this script at the bottom of your page, right before the closing body tag, or put the handler assignment in a function called onload - or if you're using jQuery there's the very elegant $(document).ready(function() { ... } );

Note that when you assign event handlers this way, it takes the functionality out of your HTML. Also note you set it equal to the function name -- no (). If you did onclick = myFunc(); the function would actually execute when the handler is being set.

And I'm curious -- you knew enough to script changing the background color, but not the text color? strange:)

Erik
wt im trying to do is changing the text color by calling the .css pages.. im facing problem in that ..i ve created three different .css pages.. but when click the the first .css page which i ve called is displaying but nt others.i ve called all the three .css file also..
Patel
@patel That would be very useful to include in the original question. You should edit it to include that important little tid-bit.
Justin Johnson