Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts

    getElementById('ID).value not working in ie8

    I have some simple javascript code that works fine in chrome and safari, but not Ie8

    Code:
    <head><script type="text/javascript">
    function val2() {
    	//var num = Number(document.getElementById('PdRows').value)
    	alert (document.getElementById('PdRows').value)
    }
    </script></head>
    
    <form action="" method="post">
    <select name="PD" id="PdRows" onchange="val2()">
      <option>1</option>
      <option>2</option>
      <option>3</option>
      <option>4</option>
      <option>5</option>
    </select>   
    </form>
    Why? And how do I correct?
    Last edited by javanew; 09-30-2011 at 09:37 PM.

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,881
    Thanks
    56
    Thanked 539 Times in 536 Posts
    actually, I'm going to side with IE8 on that one

    you're not assigning any value to your options, and then you're asking for that value to be displayed

    obviously those other browsers go looking for the value, don't find it and content themselves with the option *text* instead. IE just gives up.

    try this instead:

    Code:
    <form action="" method="post">
    <select name="PD" id="PdRows" onchange="val2()">
      <option value="1">one</option>
      <option value="2">two</option>
      <option value="3">three</option>
      <option value="4">four</option>
      <option value="5">five</option>
    </select>   
    </form>
    although if what you want is actually the option text, that's a different story

  • Users who have thanked xelawho for this post:

    javanew (09-30-2011)

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,895
    Thanks
    202
    Thanked 2,530 Times in 2,508 Posts
    To capture the text of a select option:-

    Code:
    var si = document.getElementById("PdRows").selectedIndex; 
    var txt1 = document.getElementById("PdRows").options[si].text; 
    alert (txt1);

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    javanew (09-30-2011)

  • #4
    New to the CF scene
    Join Date
    Apr 2005
    Posts
    8
    Thanks
    5
    Thanked 0 Times in 0 Posts
    That was the issue.. thanks!!!

    Quote Originally Posted by xelawho View Post
    actually, I'm going to side with IE8 on that one

    you're not assigning any value to your options, and then you're asking for that value to be displayed

    obviously those other browsers go looking for the value, don't find it and content themselves with the option *text* instead. IE just gives up.

    try this instead:

    Code:
    <form action="" method="post">
    <select name="PD" id="PdRows" onchange="val2()">
      <option value="1">one</option>
      <option value="2">two</option>
      <option value="3">three</option>
      <option value="4">four</option>
      <option value="5">five</option>
    </select>   
    </form>
    although if what you want is actually the option text, that's a different story


  •  

    Posting Permissions

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