tags:

views:

68

answers:

4

Hi all, I need to dynamicaly change css, and I'm doing that by looking at

<div id="page_title">Homepage</div> -> this element

I'm checking its innerHTML if the title is homepage then change style if page name is about change css to this and that etc.

Now this works in ff but not in IE7, what is the work arround this?
I found some links on google but nothing 100 % working and accurate. Can I get some advice on how to do this?

Thank you

+1  A: 
var page_title=document.getElementById("page_title");

if(/homepage/i.test(page_title.innerHTML))
    page_title.className="thisone";
else if(/about/i.test(page_title.innerHTML))
    page_title.className="thatone";

If your code is looks like that and if it does not work, the problem may be in CSS itself.

S.Mark
It works in FF but it doesn't work in IE 7
Gandalf StormCrow
could you post your css, and your javacsript code for that purpose or any online url to test? .className should be ok on almost all decent PC browsers. I think problem is in CSS though or javascript is failing before its reach that part.
S.Mark
A: 

The following code works on my IE7, if the content of the DIV is Homepage the background of the body turns blue. And green othewise:

<html>
<head>
    <title></title>
</head>
<body>
<div id="page_title">Homepage</div>
<script>
    if (document.getElementById('page_title').innerHTML === 'Homepage') {
        document.body.style.background = '#DEF';//blue
    } else {
        document.body.style.background = '#DFE';//green
    }
</script>
</body>
</html>

May be give here an example of your non-working code.

Mic
A: 

Maybe you have white space or invisible characters within your div, causing the comparison to fail.

Can you try trimming the innerHTML using a function like this one and comparing the result?

Pekka
A: 

Thank you for your answers I've solved the problem by using instead of div, h1 element and I didn't use innerHTML but title to get its value. Thank you

Gandalf StormCrow