Current location: Hot Scripts Forums » General Web Coding » JavaScript » Javascript Controlling CSS Question


Javascript Controlling CSS Question

Reply
  #1 (permalink)  
Old 05-17-06, 09:44 AM
nova912's Avatar
nova912 nova912 is offline
Code Guru
 
Join Date: Sep 2004
Location: Traverse City, MI, USA
Posts: 821
Thanks: 0
Thanked 0 Times in 0 Posts
Javascript Controlling CSS Question

Ok i want to crate a "simple" roll over using javascript and css.

But i cant seem to figure it out, here is my code...
Code:
<style type="text/css">
		#intro {background-color: white}
		</style>
		<script language="Javascript" type="text/javascript">
		function rollover(var)
					{
					element = document.getElementById(var);
					element.background-color = "gray";
					}
</script>
What am i missing? This is triggered by a mouseover event that calls this function like so
Code:
onmouseover="rollover('intro')"
Thanks.

Last edited by nova912; 05-17-06 at 09:44 AM. Reason: Added </script>
Reply With Quote
  #2 (permalink)  
Old 05-17-06, 10:08 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
A couple of things.

Don't use "var" as varible name.
There are no "-"s in style attributes. Use "backgroundColor" instead. (More info)
And there should be a "style" (eg: style.backgroundColor = 'grey')

This works for me

Code:
<style type="text/css">
#intro {background-color: white}
</style>

<script language="Javascript" type="text/javascript">

function rollover(obj)
{
	element = document.getElementById(obj).style;
	element.backgroundColor = "gray";
}
</script>

<div id="intro" onmouseover="rollover('intro');">Testing...</div>
Reply With Quote
  #3 (permalink)  
Old 05-17-06, 10:32 AM
nova912's Avatar
nova912 nova912 is offline
Code Guru
 
Join Date: Sep 2004
Location: Traverse City, MI, USA
Posts: 821
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks you rock!
Reply With Quote
  #4 (permalink)  
Old 04-25-09, 08:45 AM
ndthuan ndthuan is offline
Newbie Coder
 
Join Date: Apr 2009
Location: Vietnam
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Hi, I suggest another solution which decreases your HTML

Code:
<style type="text/css">
#intro {background-color: white}
</style>

<script language="Javascript" type="text/javascript">

function rollover(obj)
{
	obj.style.backgroundColor = "gray";
}
</script>

<div id="intro" onmouseover="rollover(this);">Testing...</div>
Reply With Quote
  #5 (permalink)  
Old 04-26-09, 07:45 AM
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
Instead of using the "id" of the element, you could assign a "class" to the element and then use the mouse events to change the class name.

In this little program, when you mouse over an object it turns gray.
And when you mouse out, the object turns white again unless you click on the object, it will turn another color until you click on it again.

This would be useful if you wish to select only certain objects.
Javascript Code:
  1. <html>
  2. <head>
  3. <style type="text/css">
  4. .white {background-color: white}
  5. .gray {background-color: gray}
  6. .red {background-color: red}
  7. .green {background-color: green}
  8. .blue {background-color: blue}
  9. #div1, #div2, #div3
  10. {
  11.  width:100px;
  12.  border:1px solid #000;
  13.  margin-bottom:5px;
  14.  }
  15. #div4 {margin-top:10px;}
  16. </style>
  17. <script language="Javascript" type="text/javascript">
  18. function rollover(obj, v)
  19. {
  20.  document.getElementById("div4").style.display = "block";
  21.  document.getElementById("rec").innerHTML = "";
  22.  switch(v)
  23.  {
  24.   case 0:
  25.    if(obj.className == "gray"){obj.className = "white";}
  26.    break;
  27.   case 1:
  28.    if(obj.className == "white"){obj.className = "gray";}
  29.    break;
  30.   case 2:
  31.    obj.className = obj.className == "red" ? "gray" : "red";
  32.    break;
  33.   case 3:
  34.    obj.className = obj.className == "green" ? "gray" : "green";
  35.    break;
  36.   case 4:
  37.    obj.className = obj.className == "blue" ? "gray" : "blue";
  38.    break;
  39.   }
  40.  }
  41. function display_selected_objects(v)
  42. {
  43.  document.getElementById("div4").style.display = "none";
  44.  var output = "<p>";
  45.  var obj = v.split(",");
  46.  for(var i = 0; i < obj.length; i++)
  47.  {
  48.   if(document.getElementById(obj[i]).className != "white"){output +=  "<span style='color:blue;'>" + document.getElementById(obj[i]).innerHTML + " is selected.</span><br />";}
  49.   else{output +=  "<span style='color:red;'>" + document.getElementById(obj[i]).innerHTML + " is not selected.</span><br />";}
  50.   }
  51.  document.getElementById("rec").innerHTML = output;
  52.  }
  53. </script>
  54. </head>
  55. <body>
  56. <div id="div1" class="white" onclick="rollover(this,2);" onmouseover="rollover(this,1);" onmouseout="rollover(this,0);">Testing...1</div>
  57. <div id="div2" class="white" onclick="rollover(this,3);" onmouseover="rollover(this,1);" onmouseout="rollover(this,0);">Testing...2</div>
  58. <div id="div3" class="white" onclick="rollover(this,4);" onmouseover="rollover(this,1);" onmouseout="rollover(this,0);">Testing...3</div>
  59. <div id="div4"><a href="#" onclick="display_selected_objects('div1,div2,div3');">Display selected objects.</a></div>
  60. <div id="rec"></div>
  61. </body>
  62. </html>
__________________
Jerry Broughton

Last edited by job0107; 04-26-09 at 07:50 AM.
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
using css or javascript for paging nav in php page sagaquisces CSS 1 12-23-05 02:10 PM
CSS Question Justin171985 CSS 1 06-04-05 04:00 AM
Question on using Javascript and PHP ajs JavaScript 0 04-20-05 07:11 PM
Huge Javascript question and help... CBRyder13 JavaScript 8 02-11-05 04:54 AM
question about forms using javascript jacven JavaScript 9 03-05-04 10:34 AM


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