SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Code won't work in IE

    Hi,

    The following code won't work in IE. The alert reports x.length as zero. How can fix this?

    Code:
    function hideAll(name) {
    	var x=document.getElementsByName(name);
    	alert(x.length);
    	for (var i=0;i<x.length;i++)
    	{
    		x[i].style.display='none';
    	}
    }
    David

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    What is the HTML code that the JavaScript is interfacing with?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's just a bunch of div's like so:

    Code:
    <a href="javascript:void(0)l" onclick="hideAll('toHide');">Hide All</a>
    <div>Some content</div>
    <div name="toHide">Sub content here</div>
    <div>Some more content</div>
    <div name="toHide">Sub content here</div>
    <div>Some other content</div>
    <div name="toHide">Sub content here</div>
    David

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Me01273 View Post
    It's just a bunch of div's like so
    Thanks.

    Internet Explorer is broken when it comes to using getElementsByName
    The name attribute should only be used to specify form fields that are to be submitted.

    In the way you are attempting to use it, you will be much better served by using the class attribute instead.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    133
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for helping.

    I can't seem to find any native function for working on class attributes though. I kind of expected there to be a getElementByClass(), but alas there is not. How could I implement a function that performs similar function to mine but IE compatible?

    David

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,705
    Mentioned
    102 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Me01273 View Post
    Thanks for helping.

    I can't seem to find any native function for working on class attributes though. I kind of expected there to be a getElementByClass(), but alas there is not. How could I implement a function that performs similar function to mine but IE compatible?
    There are a couple of ways.

    Historically you would use these class handling functions which gives you hasClass(), addClass() and removeClass()

    However, there is an improved technique nowdays by using querySelector() instead.

    Code javascript:
    var elems = document.querySelector('toHide');
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •