Jump to content

newprogrammingfan
Go to solution Solved by Irate,

Recommended Posts

Hi!

I am new at this,so can anyone show or tell me how can i make a text that changes color all the time when i click on it.When first time i click on the black text ->

it changes to Red,when i click again it become black again and if i click again it becomes Red and so on,This in a circle.How can i do this?

Thanks for the help and please no flaming i am new yet,and didn't found my answer on the net yet...

Any help or idea welcome!

Link to comment
Share on other sites

Assuming you have an element that looks like this.

 

<span id="colorChange">Clicking this text changes the color of it.</span>
Script below.

 

window.onload = function(){
 var cc = document.getElementById("colorChange");
 if( !cc ) {
  throw new Error("Element to change color was not found.");
 }
 cc.onclick = function(){
  if( cc.style.color == "rgb(255,0,0)" ) cc.style.color = "rgb(0,0,0)";
  else cc.style.color = "rgb(255,0,0)";
 };
};
That's about all you need. You could use jQuery and make this a bit more simple and also being able to fire the script without having to wait for images to completely load.
Link to comment
Share on other sites

Assuming you have an element that looks like this.

 

<span id="colorChange">Clicking this text changes the color of it.</span>
Script below.

 

window.onload = function(){
 var cc = document.getElementById("colorChange");
 if( !cc ) {
  throw new Error("Element to change color was not found.");
 }
 cc.onclick = function(){
  if( cc.style.color == "rgb(255,0,0)" ) cc.style.color = "rgb(0,0,0)";
  else cc.style.color = "rgb(255,0,0)";
 };
};
That's about all you need. You could use jQuery and make this a bit more simple and also being able to fire the script without having to wait for images to completely load.

 

 

Thanks!But it won't change the color back so i made a modification,but still not works,can anyone help with it?

 

Files and their contents are below->

 

textcolor.php:

<html>
<head>
<script src="jquerry.js"></script>
</head>
<body>
<span id="colorChange">Clicking this text changes the color of it.</span>
</body>
</html>

jquerry.js:

window.onload = function(){
 var cc = document.getElementById("colorChange");
 var dd = document.getElementById("colorChange");
 if( !cc ) 
 cc.onclick = function(){
  if( cc.style.color == "rgb(255,0,0)" )
  cc.style.color = "rgb(0,0,0)";
  };
 if( !dd )
 dd.onclick = function(){
  if( cc.style.color = "rgb(0,0,0)" )
  dd.style.color = "rgb(255,0,0)";
 };
};
Link to comment
Share on other sites

I tryed with button,but still no succes,some help to a beginner please? :)

<!DOCTYPE html>
<html>
<head>
<script>

    document.getElementById('change').onclick = changeColor;   
    function changeColor() {
      
     if(document.getElementById('color').style.color == "black";)
        {
        document.getElementById('color').style.color = "blue";
        }
     else
        {
        document.getElementById('color').style.color = "black";
        }
                           }  

</script>
</head>
<body>
<script type="text/javascript">
 document.getElementById('color').style.color="black";
</script>

<button onclick="changeColor">Color Change</button>
<p id="color">Hey there!</p>


</body>
</html>

Edited by newprogrammingfan
Link to comment
Share on other sites

  • Solution

My bad, I figured out the problem. The elem.style.color property is always noted as rgb value with spaces, I left out the spaces.

 

Below script should work just fine.

 

 

window.onload = function(){
 var cc = document.getElementById("colorChange");
 if( !cc ) {
  throw new Error("Element to change color was not found.");
 }
 cc.onclick = function(){
  if( cc.style.color == "rgb(255, 0, 0)" ) cc.style.color = "rgb(0, 0, 0)";
  else cc.style.color = "rgb(255, 0, 0)";
 };
};
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.