Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

beginners problem styling using an id

Status
Not open for further replies.

daantje

Programmer
May 9, 2007
5
NL
For my website I had to add javascript with my PHP site in order to get the anchor tag from the current page. This in order to compare it to the id of an <span> tag in the page. Javascript is not my strong side. So of course the script doesn't work.This is the problem:


Here is my PHP/javascript code:


Code:
$output .= "<script type=\"text/javascript\">
if( \"#term{$term->tid}\" == location.hash){
document.all[\"term{$term->tid}\"].style.color=\"red\"
</script>";
$output .= "<span id=\"term{$term->tid}\">{$term->name}</span>";


This code looks as folows in the source code of the page......


Code:
<script type="text/javascript">
  if( "#term1" == location.hash){
    document.all["term1"].style.color="red"
  }
</script>
<span id="term1">Termname</span>


This all worked fine when I used document.write instead of the line styling the term....so the line.....
Code:
document.all[\"term{$term->tid}\"].style.color=\"red\"
otherwise
Code:
document.all["term1"].style.color="red"
.....is causing the trouble.

It looks fine to me...I have no idea what went wrong because the "Termname" does not turn red. Anyone have any ideas??

Thanx
 
instead of using this line
Code:
document.all["term1"].style.color="red"
change it to
Code:
document.getElementById("term1").style.color = "#ff0000";

I don't think FF understands document.all
PHP
Code:
document.getElementById(\"term{$term->tid}\").style.color=\"#ff0000\"

if that doesn't change it red, then put an alert
Code:
alert("#term1" == location.hash);



[monkey][snake] <.
 
Hi

monksnake said:
I don't think FF understands document.all
Actually it does, but only in Quirks Mode, just like Opera and Konqueror. They all were forced to this step by the huge amount of webmasters unaware of web standards. :-(

Feherke.
 
I am using FireFox so that's probably it. I tried using

Code:
<script type=\"text/javascript\">
if( \"#term{$term->tid}\" == location.hash){
document.getElementById(\"term{$term->tid}\").style.color=\"#ff0000\";
}
</script>";

But that didn't work either. When I put the alert in I get a true as alert. So it is the line..

Code:
document.getElementById(\"term{$term->tid}\").style.color=\"#ff0000\";

Any suggestions?
thanx
 
Here is a suggestion. Does this element exist yet when you run the script to turn it red?

Code:
<span id="term1">Termname</span>

I'm guessing no. Put that script anywhere physically below the <span>.

[monkey][snake] <.
 
Actually it does....see the top of the forum.

Could it have anything to do with the fact that I already assigned a color to it using css. But this loads before the javascript does right...so that couldn't be it...could it?
 
Actually it does....see the top of the forum

I see this:

Code:
<script type="text/javascript">
  if( "#term1" == location.hash){
    document.all["term1"].style.color="red"
  }
</script>
<span id="term1">Termname</span>

This would not work.

Code:
<span id="term1">Termname</span>
<script type="text/javascript">
  if( "#term1" == location.hash){
    document.all["term1"].style.color="red"
  }
</script>

This would.

[monkey][snake] <.
 
Could it have anything to do with my CMS....I use Drupal..it might be in conflict or somthing...I don't get any errors though
 
Could it have anything to do with my CMS....I use Drupal

To be honest I have no idea what Drupal is.

I saw above you said something about your CSS definition, you can try taking that out to see if anything happens.

There is a conflict somewhere, cause this is elementary (making the font of a <span> a different color)

It could also be possible you have 2 elements with the same id.

I'm just throwing stuff out there, cause I'm not sure what the conflict is.

[monkey][snake] <.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top