SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    What Is Wrong With This Simple Javascript?

    Why doesn't this work?

    Code:
    <script>
    function xxx()
    {
    document.getElementByID('divname').innerHTML="changed";
    }
    </script>
    
    <div id="divname">
    start
    </div>
    
    <a href='javascript:xxx()'>click</a>

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    When the browser parses "document.getElementById('divname'), divname doesn't exist yet. You have to put the Javascript after it. Preferably all together at the end of the body, before the closing </body> tag. It will also fail because document.getElementById is case sensitive and you've got it slightly wrong.

    You should not be using a link like that (using javascript:xxx()). I suggest you type "unobtrusive javascript" into google and have a read. Also, the convention is to use double quotation marks for attributes always.

  3. #3
    SitePoint Addict
    Join Date
    Sep 2008
    Posts
    341
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent response. Thank you.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,701
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    To clarify that quotation convention, HTML code should always use double quotes, and javascript code should use single quotes where possible.

    Code html4strict:
    <!-- bad quotes -->
    <a id='updateDiv' href='#'>click</a> 
     
    <!-- good quotes -->
    <a id="updateDiv" href="#">click</a>

    Code javascript:
    var foo = "This is some text"; // bad quotes
    var bar = 'This is some text'; // good quotes
     
    var baz = 'Here\'s more text'; //  unacceptable - less readable
    var bat = "Here's more text"; // acceptable - a good compromise
    Last edited by paul_wilkins; Mar 5, 2009 at 08:47.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Evangelist bradical1379's Avatar
    Join Date
    Feb 2007
    Posts
    442
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    document.getElementByID('divname').innerHTML="changed";
    getElementByID is incorrect, it needs to be getElementById.

    Code:
    document.getElementById('divname').innerHTML="changed";

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    ... but using single quotation marks, for convention's sake:
    Code:
    document.getElementById('divname').innerHTML = 'changed';


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •