Easy Javascript question (getting dropdown list values)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yarbrough40
    Contributor
    • Jun 2009
    • 320

    Easy Javascript question (getting dropdown list values)

    I'm a vb.net user mostly... can anyone tell me why I'm getting a runtime error 'permission denied' when running this? trying to get values from a dropdownlist.

    Code:
     
    function getvalues()
    {
    var dl = document.getElementById("DropDownList11");
    var item1;
    
    item1 = dl.options[0].value;
    document.write(item1 + "<br>");
    
    item1 = dl.options[1].value;      //error occurrs here
    document.write(item1 + "<br>");
    }
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    I think that happens when you try to use document.write( ) on an already loaded document. document.write( ) is not suited to modify a HTML page.

    Comment

    • yarbrough40
      Contributor
      • Jun 2009
      • 320

      #3
      thanks Dormilich, however that's not the answer. I can tell because this simple code executes without error...
      Code:
      function getvalues()
      {
      var string = "hello" + "<br>";
      document.write(string); 
      
      string = "world";
      document.write(string); 
      }
      in my first example I show exactly where the error occurs and it's at the second time I use 'document.getEl ementById'... is there perhaps some kind of fundamental Java violation when attempting to get values from an object more than once?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        in my first example I show exactly where the error occurs and it's at the second time I use 'document.getEl ementById'...
        sometimes the line that throws an error is not the line that causes it…

        anyway, I can’t see you using getElementById( ) twice.

        if you can provide a link, I’ll have my FireBug peek at it.

        Comment

        • yarbrough40
          Contributor
          • Jun 2009
          • 320

          #5
          I figured it out and you are mostly correct! you can use document.write as often as you like. the problem is that I can pull the first value from the dropdown control but once I document.write that value to the new page, I am unable to reference that same control because it no longer exists. in this case I must compile all of my values first then write to the page...

          thanks for pointing me in the right direction. problem solved!

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            in this case I must compile all of my values first then write to the page...
            …or use a more appropriate method like DOM.

            Comment

            • gits
              Recognized Expert Moderator Expert
              • May 2007
              • 5390

              #7
              Originally posted by Dormilich
              …or use a more appropriate method like DOM
              that way is definitly to prefer, document.write( ) shouldn't be used or when it has to ... then at least with the corresponding open() and close() methods.

              kind regards

              Comment

              • yarbrough40
                Contributor
                • Jun 2009
                • 320

                #8
                thanks - I really have no reason to use document.write. this was only used as a test to see if I could get at the values. it is a common practice in .net using response.write. the difference is that response.write does not instantiate a new page so the data objects you reference will persist and no errors will occur. It's a simple matter of understanding my environment : )

                Comment

                • acoder
                  Recognized Expert MVP
                  • Nov 2006
                  • 16032

                  #9
                  Originally posted by yarbrough40
                  is there perhaps some kind of fundamental Java violation...
                  Sorry to be picky, but it's important. Java and JavaScript are two very different languages. It's important to make the distinction.

                  Comment

                  Working...