Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Change style by ID

Reply
Thread Tools

Change style by ID

 
 
BillyZ
Guest
Posts: n/a
 
      07-10-2008
I have a navigation panel on 20 or so page and would like to change ot
to a include. My problem is that the link of the current page has to
be bold. I believe I am close but the code below doesn't work.

Any help would be appreciated.

Thanks

<script>
var pageName = "premiumSupport";

function changeBold() {
if (pageName == 'premiumSupport') {
document.getElementById('overview').style.fontWeig ht='bold';
}
}
changeBold ();
</script>

<a id="overview" href="/customercare/index.htm">Overview</a>

 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      07-10-2008
BillyZ wrote:
>I have a navigation panel on 20 or so page and would like to
> change ot to a include. My problem is that the link of the
> current page has to be bold. I believe I am close but the
> code below doesn't work.
>
> Any help would be appreciated.
>
> Thanks
>
> <script>
> var pageName = "premiumSupport";
>
> function changeBold() {
> if (pageName == 'premiumSupport') {
> document.getElementById('overview').style.fontWeig ht='bold';
> }
> }
> changeBold ();
> </script>
>
> <a id="overview" href="/customercare/index.htm">Overview</a>


If this is representative of the structure of your HTML (which pretty
much has to be assumed to be the case here) then your problem is that
the global code in SCRIPT elements is executed immediately after the
SCRIPT element is parsed by the HTML parser and the A element cannot
exist in the DOM prior to HTML parser parsing the A element's mark-up.
Thus your - changeBold(); - happens before the DOM contains the A
element, and so - document.getElementById('overview') - must fail to
find that element.

The solution is probably to move the - changeBold(); - call into an
onload handler for the page.

Richard.

 
Reply With Quote
 
 
 
 
BillyZ
Guest
Posts: n/a
 
      07-10-2008
On Jul 10, 4:14*pm, "Richard Cornford" <(E-Mail Removed)>
wrote:
> BillyZ wrote:
> >I have a navigation panel on 20 or so page and would like to
> > change ot to a include. My problem is that the link of the
> > current page has to be bold. I believe I am close but the
> > code below doesn't work.

>
> > Any help would be appreciated.

>
> > Thanks

>
> > <script>
> > var pageName = "premiumSupport";

>
> > function changeBold() {
> > * *if (pageName == 'premiumSupport') {
> > * * * *document.getElementById('overview').style.fontWeig ht='bold';
> > * * * *}
> > * *}
> > changeBold ();
> > </script>

>
> > <a id="overview" href="/customercare/index.htm">Overview</a>

>
> If this is representative of the structure of your HTML (which pretty
> much has to be assumed to be the case here) then your problem is that
> the global code in SCRIPT elements is executed immediately after the
> SCRIPT element is parsed by the HTML parser and the A element cannot
> exist in the DOM prior to HTML parser parsing the A element's mark-up.
> Thus your - changeBold(); - happens before the DOM contains the A
> element, and so - document.getElementById('overview') - must fail to
> find that element.
>
> The solution is probably to move the - changeBold(); *- call into an
> onload handler for the page.
>
> Richard.- Hide quoted text -
>
> - Show quoted text -


Thank you for taking the time to respond. One additional question. I
am working within a cms system and do not have access to the body tag.
The links all live within a div can onLoad go in a div and if so how?

Thanks
 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      07-11-2008
BillyZ wrote:
> On Jul 10, 4:14 pm, Richard Cornford wrote:

<snip>
>> The solution is probably to move the - changeBold(); - call into
>> an onload handler for the page.

>
> Thank you for taking the time to respond. One additional question.
> I am working within a cms system and do not have access to the
> body tag. The links all live within a div can onLoad go in a div
> and if so how?



A sensible CMS system that anticipate scripting may make provision for
adding onload handlers. You cannot use an HTML onload attribute anywhere
but in the opening BODY tag, but you can assign a function to -
window.onload - from any SCRIPT element. I.E.:-

window.onload = function(){
changeBold();
};

- in place of - changeBold(); - should work. However, if any earlier
SCRIPT element had done the same then that action will replace the
pre-existing onload handler. If the opening BODY tag contains an onload
attribute then the parsing of that attribute will replace any -
window.onload - handler assigned before it is parsed, and be replaced by
any - window.onload - assignment that happens after it is parsed. And
any subsequent assignment to - widnnow.onload - in later SCRIPT elements
would replace your handler.

One way around that is to use (branching depending on the method's
existence) - attachEvent - (IE and close imitators) or -
addEventListener - (some DOM standard browsers) methods, which allow
multiple functions to be associated with events with no danger of
overriding pre-existing handlers. If you Google with the method names
(both of them) and 'load' as keywords you will probably find numerous
examples of how they could be used in this context.

Richard.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A Paradise DNS address change? What change? There was no change. Tony Neville NZ Computing 7 09-22-2006 01:02 PM
Opera won't change the style information when I change classname torbs Javascript 2 05-02-2006 07:29 AM
DataGrid header style inconsistent with sortable column style cedoucette@alum.rpi.edu ASP .Net 0 10-14-2005 12:13 AM
All style tags after the first 30 style tags on an HTML page are not applied in Internet Explorer Rob Nicholson ASP .Net 3 05-28-2005 03:11 PM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM



Advertisments