document.getElementById problem

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Jan 2008
Posts: 19
Reputation: Begjinner is an unknown quantity at this point 
Solved Threads: 0
Begjinner's Avatar
Begjinner Begjinner is offline Offline
Newbie Poster

document.getElementById problem

 
0
  #1
Feb 19th, 2008
Hello,

I have this in my CSS:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. #contentbox a{
  2. font-weight: bold;
  3. color: #c8a468;
  4. cursor: pointer;
  5. }

And want to change the color with Javascript's function: document.getElementById, but when I use document.getElementById('contentbox a').style.color= "#d05048", I get an error that it doesn't know 'contentbox a'.

I tried multiple things and searched on google, but I cannot get it to work.

Who can help me? It's a simple question...
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 154
Reputation: katarey is an unknown quantity at this point 
Solved Threads: 20
katarey's Avatar
katarey katarey is offline Offline
Junior Poster

Re: document.getElementById problem

 
0
  #2
Feb 19th, 2008
Hi there,

you have made mistake here

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. document.getElementById('contentbox a').style.color= "#d05048"

that should look like

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. document.getElementById('contentbox').style.color= "#d05048"

Example:

CSS:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <style type="text/css">
  2. #contentbox, #contentbox a{
  3. font-weight: bold;
  4. color: #c8a468;
  5. cursor: pointer;
  6. }
  7. </style>
JavaScript:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script type="text/javascript">
  2. function changeColor(){
  3. var divID = document.getElementById('contentbox');
  4. divID.style.color= "#d05048";
  5. }
  6. </script>

HTML:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <div id="contentbox">
  2. <a href="javascript:void(0);" onClick="changeColor()">Link Text</a> Other Text </div>

hope this will help you

Rahul Dev
Freelance Web Designer & Developer
Http//www.Katarey.com
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 19
Reputation: Begjinner is an unknown quantity at this point 
Solved Threads: 0
Begjinner's Avatar
Begjinner Begjinner is offline Offline
Newbie Poster

Re: document.getElementById problem

 
0
  #3
Feb 19th, 2008
Hi, thanks for your help, but the 'a' is there on purpose. I want to change the colour of links only in the div 'contentbox' (with javascript).
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 483
Reputation: DangerDev has a spectacular aura about DangerDev has a spectacular aura about 
Solved Threads: 58
DangerDev's Avatar
DangerDev DangerDev is offline Offline
Posting Pro in Training

Re: document.getElementById problem

 
0
  #4
Feb 20th, 2008
hi you can not do directly like this, the way you have tried is wrong.
getElementById() takes only id.

use elem.getElementsByTagName('tag name');
this will return array of element('tag name') inside elem.
using loop you can...........
i think this much is enough.
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 21
Reputation: aravelli is an unknown quantity at this point 
Solved Threads: 2
aravelli aravelli is offline Offline
Newbie Poster

Re: document.getElementById problem

 
0
  #5
Feb 26th, 2008
<style content="text/css">
#contentbox a{
font-weight: bold;
color: #c8a468;
cursor: pointer;
}
</style>
<script content="text/javascript">
function text_over(){
document.getElementById('contentbox a').style.color= "#d05048";
}
function text_out(){
document.getElementById('contentbox a').style.color= "#c8a468";
}
</script>
</HEAD>

<BODY>
<div id="contentbox">
<a href="......" id="contentbox a" onMouseover="text_over()" onMouseout="text_out()">infinite</a>
</div>
</BODY>


i think this may help you.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 19
Reputation: Begjinner is an unknown quantity at this point 
Solved Threads: 0
Begjinner's Avatar
Begjinner Begjinner is offline Offline
Newbie Poster

Re: document.getElementById problem

 
0
  #6
Feb 26th, 2008
aravelli, that works, thank you, but only for the first one containing the id. On some pages I have more links and now only the first one changes.

this is what I have:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. try{document.getElementById('contentbox a').style.color= "#d05048";}catch(err){};

there is a try...cath function because some pages don't have the 'contentbox a' id.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 21
Reputation: aravelli is an unknown quantity at this point 
Solved Threads: 2
aravelli aravelli is offline Offline
Newbie Poster

Re: document.getElementById problem

 
0
  #7
Feb 26th, 2008
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<style type="text/css">
a:link {
color: #c8a468;
text-decoration: none;
}
a:visited {
color: #d05048;
text-decoration: none;
}
a:hover {
color: #666666;
text-decoration: none;
}
a:active {
color: #84414A;
text-decoration: none;
}</style>
</HEAD>

<BODY>
<div id="contentbox">
<a href="......">text goes here</a><br>
<a href="......">text goes here</a><br>
<a href="......">text goes here</a>
</div>
</BODY>
</HTML>


why can't you go like this

instead of using javascript

i think this will help for ur need
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 19
Reputation: Begjinner is an unknown quantity at this point 
Solved Threads: 0
Begjinner's Avatar
Begjinner Begjinner is offline Offline
Newbie Poster

Re: document.getElementById problem

 
0
  #8
Feb 26th, 2008
I have links in the contentbox and I have links in my menu. When I use CSS to change "a" the menu gets the wrong colours and cannot be fixed with div's because the "a" tag comes as last and so overwrites the rest.

I want to only change the link colours in the contentbox but javascript doesn't understand the id "contentbox a" because it's the id "a" in the id "contentbox" .

When I gave the links an id (all the same id) it worked, but javascript only changed the first id, while there are more on some pages. Maybe it can be done with a while loop, but I have no idea how.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 21
Reputation: aravelli is an unknown quantity at this point 
Solved Threads: 2
aravelli aravelli is offline Offline
Newbie Poster

Re: document.getElementById problem

 
0
  #9
Feb 26th, 2008
<style type="text/css">
#contentbox a:link {
color: #c8a468;
text-decoration: none;
}
#contentbox a:visited {
color: #d05048;
text-decoration: none;
}
#contentbox a:hover {
color: #666666;
text-decoration: none;
}
#contentbox a:active {
color: #84414A;
text-decoration: none;
}</style>

change the css like this
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 3,210
Reputation: MidiMagic has a spectacular aura about MidiMagic has a spectacular aura about 
Solved Threads: 165
MidiMagic's Avatar
MidiMagic MidiMagic is offline Offline
Nearly a Senior Poster

Re: document.getElementById problem

 
0
  #10
Feb 27th, 2008
An id can be used only once. It can NOT appear in multiple tags.

You need to give each tag a unique id, and then cycle through the ids with a loop in js.

e.g.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <a .... id="aa1">
  2. <a .... id="aa2">
  3. <a .... id="aa3">
  4. <a .... id="aa4">
  5. <a .... id="aa5">
Daylight-saving time uses more gasoline
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC