Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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
Reply With Quote
  #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.
Reply With Quote
  #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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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


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



All times are GMT -4. The time now is 09:24 PM.


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