Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 22nd, 2007, 01:21 PM
Registered User
 
Join Date: Feb 2007
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Difficulty writing javascript to access dom

Hi There;

I am trying to add some onClick functionality to a group of menu items in a template-based site by creating a javascript function that locates the a tag in this group and adds a new attribute.

It's not working :( Could someone please be kind to look at what i've written and tell me what i'm doing wrong?

Header script:

Code:
<script type="text/javascript">
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addAtt() {
  if ( !document.getElementById ||
        !document.createElement ||
        !document.getElementsByTagName ) {
        return;
    }
  var primaryDiv = document.getElementById("navlist");
  var primarylinks = primaryDiv.getElementsByTagName("a");
  for ( var i = 0; i < primarylinks.length; i++ ) {

    primarylinks[i].setAttribute("onClick","Effect.Appear('d6')");

  }
}
addLoadEvent(addAtt);
</script>
and in the body:
Code:
<div class="secondnav">
    <ul class="links" id="navlist">
        <li class="first menu-2-1-49">
            <a href="#" class="menu-2-1-49">style</a></li>
    </ul>
</div>

<div class="secondnav" id="d6" style="display:none">
    <ul class="links" id="subnavlist">
        <li class="first menu-2-1-49">
            <a href="/dave/node/3" class="menu-2-1-49">style</a></li>
    </ul>
</div>
thanks
  #2 (permalink)  
Old February 23rd, 2007, 07:32 AM
Friend of Wrox
 
Join Date: May 2004
Location: , , .
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Not sure but something jumped out at me straight away:

this line:

if (typeof window.onload != 'function') {

Should surely be:

if (typeof(window.onload) != 'function') {

Not even sure that will work TBH.
HTH
Joe


'sync' <cr>
The name specified is not recognized as an internal or external command, operable program or batch file.
  #3 (permalink)  
Old February 23rd, 2007, 11:25 AM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

You actually don't have to place parenthesis around window.onload. typeof, as a language construct does not require these, since it is not a function.

Personally, I always put those in, but they're technically optional.

Regards,
Rich

--
Author,
Beginning CSS: Cascading Style Sheets For Web Design
CSS Instant Results

http://www.catb.org/~esr/faqs/smart-questions.html


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in writing anything within "" by XML DOM. Somesh XML 0 March 5th, 2007 07:45 AM
Problem in writing within "" by DOM. Somesh XSLT 1 March 5th, 2007 07:27 AM
Problem in writing anything within "" by XML DOM. Somesh C# 0 March 5th, 2007 07:08 AM
Javascript Confirm dialog difficulty ayrton Javascript 1 May 20th, 2005 03:34 AM
Problem writing xml file using DOM Bala XML 0 September 6th, 2003 02:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.