Please Help

Discussion in 'JavaScript' started by Darkhodge, Mar 19, 2007.

  1. #1
    Hi,

    I have the following:

    
    <ul>
      <li id = "example" style="width: 50%; float: left; padding: 0; clear: none; margin: 0; display: inline;"><span style="padding: 3px; display: block; font-size: 10px; width: 100%; color: #000000; margin: 0;">Example</li>
    </ul>
    
    Code (markup):
    And the following javascript code that runs on a specific event:

    
    <script type = "text/javascript">
    <!-- Hide from older browsers
    
    function changeAdsPerRow(){
    
      window.document.getElementByID('example').style.width = "25%";
    
    }
    // End hide -->
    </script>
    
    Code (markup):
    When the function is run I get an error message saying "Object does not support this property or method".

    What am I doing wrong and how do I fix this?


    Thanks,

    Hodge :)
     
    Darkhodge, Mar 19, 2007 IP
  2. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #2
    Try using
    document.getElementByID('example').style.width = "25%";

    instead of
    window.document.getElementByID('example').style.width = "25%";

    PS: use a descriptive title of post.
     
    designcode, Mar 19, 2007 IP
  3. AndrewR

    AndrewR Peon

    Messages:
    108
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The id of the HTML code is "example1", however in your JS, it's "example". Also, I would just make the HTML look like id="example1" without the spaces, as it's better markup (possibly could be causing the problem also, not sure).
     
    AndrewR, Mar 19, 2007 IP
  4. Darkhodge

    Darkhodge Well-Known Member

    Messages:
    2,111
    Likes Received:
    76
    Best Answers:
    1
    Trophy Points:
    185
    #4
    Tried that and it didn't do anything...

    And sorry about the title! :eek:

    That was just a typo in pasting it across to here - sorry! That's not the root of the problem though as it's "example" throughout the original code... :eek:
     
    Darkhodge, Mar 19, 2007 IP
  5. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #5
    Yep AndrewR is right, I missed that part, now the JS will be

    document.getElementByID('example1').style.width = "25%";
     
    designcode, Mar 19, 2007 IP
  6. ajsa52

    ajsa52 Well-Known Member

    Messages:
    3,426
    Likes Received:
    125
    Best Answers:
    0
    Trophy Points:
    160
    #6
    Try with this Cross Browser function to get the style of the 'id':

    
    function getStyleObject(objectId) 
    {
      if (document.getElementById && document.getElementById(objectId)) 
        { return document.getElementById(objectId).style; } 
      else if (document.all && document.all(objectId)) 
        { return document.all(objectId).style; } 
      else if (document.layers && document.layers[objectId])
        { return document.layers[objectId]; } 
      else { return false; }
    }
    
    example_style = getStyleObject("example");
    example_style.width = "25%";
    
    
    Code (markup):
     
    ajsa52, Mar 20, 2007 IP