Javascript Error (Line: 1 Char: 1 Error: Object Expected Code: 0)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • whatelyb
    New Member
    • May 2010
    • 5

    Javascript Error (Line: 1 Char: 1 Error: Object Expected Code: 0)

    I am having trouble related to the attached coding.

    I am trying to make the object "flight1pic " change depending on what is selected in the drop down menus "airline1" and "aircraft1" .

    My problem is when changing "airline1" it all works fine, but when changing "aircraft1" i get the error:

    Line: 1
    Char: 1
    Error: Object Expected
    Code: 0

    Code:
    function flightpicFunction(){
    	if(document.getElementById('airline1').value == "Qantaslink" && document.getElementById('aircraft1').value == "Boeing 717"){ document.getElementById('flight1pic').innerHTML = "<img src=../images/aircraft/QF_B717.jpg>"; }
    	if(document.getElementById('airline1').value == "Skywest" && document.getElementById('aircraft1').value == "Fokker 100"){ document.getElementById('flight1pic').innerHTML = "<img src=../images/aircraft/XR_F100.jpg>"; }
    }
    Can anyone tell me what i might be doing wrong?

    Code:
    <select size="1" onChange="flightpicFunction();" name="aircraft1" tabindex="3">
      <option value="BAe 146">BAe 146</option>
      <option value="Boeing 717">Boeing 717</option>
      <option value="Boeing 737">Boeing 737</option>
      <option value="Dash 8">Dash 8</option>
      <option value="Embraer 120">Embraer 120</option>
      <option value="Embraer 145">Embraer 145</option>
      <option value="Embraer 170">Embraer 170</option>
      <option value="Embraer 190">Embraer 190</option>
      <option value="Fokker 100">Fokker 100</option>
      <option value="Fokker 50">Fokker 50</option>
      <option value="Other">Other</option>
    </select>
    Attached Files
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I've moved your question from the Java forum into the JavaScript forum where you will find more help with your JavaScript question.


    Select HTML elements consist of a bunch of options.
    It doesn't make sense to set a select HTML element's "value" property because it doesn't have a value....it has a bunch of options.

    -Frinny

    Comment

    • whatelyb
      New Member
      • May 2010
      • 5

      #3
      Originally posted by Frinavale
      I've moved your question from the Java forum into the JavaScript forum where you will find more help with your JavaScript question.


      Select HTML elements consist of a bunch of options.
      It doesn't make sense to set a select HTML element's "value" property because it doesn't have a value....it has a bunch of options.

      -Frinny
      Sorry, can you explain it to me in lamens (easy) terms? I'm good with PHP but I really haven't the slightest clue with Javascript.

      Comment

      • whatelyb
        New Member
        • May 2010
        • 5

        #4
        I have tried replacing the code:
        Code:
        document.getElementById('aircraft1').value
        with this code:
        Code:
        document.navform.aircraft1.options[document.navform.aircraft1.selectedIndex].value
        And I still get the same error. :(

        Comment

        • whatelyb
          New Member
          • May 2010
          • 5

          #5
          Sorry to mess you around. The problem turned out to be my PHP coding.

          The PHP coding I had set up changed which javascript function the dropdown menu called up, to one that didn't exist.

          All works fine now. Thanks.

          Comment

          • thesmithman
            New Member
            • Aug 2008
            • 37

            #6
            You are on the right track with selectedIndex.

            try something like:

            Code:
            var dropdown = document.forms[0].aircraft1;
            var selection = dropdown[dropdown.selectedIndex].value;
            Here's a reference for more information:
            W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.


            And by the way, I highly recommend doing your JavaScript debugging in a different browser. Those Internet Explorer error messages just aren't that helpful. There is a handy plugin for Firefox called Firebug; and there are developer tools built in to the Chrome browser.

            Happy coding!

            Comment

            • thesmithman
              New Member
              • Aug 2008
              • 37

              #7
              I see you got it sorted out before I hit the "post reply" button! Glad to hear it.

              Comment

              • whatelyb
                New Member
                • May 2010
                • 5

                #8
                Yeah thanx anyway "thesmithma n" and also "Frinavale" . I suppose the best diagnosis is a good night sleep. Was trying to figure it all out at 2am in the morning (before I relised what time it was.)

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  Internet Explorer 8 comes with a debugger (hit F12).
                  If you like FireFox then download a plugin called FireBug.

                  -Frinny

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    two things from my side.

                    your <select> does not have an ID, so trying to access it with getElementById( ) will only work in IE (given the name is unique, too).

                    second, a <select> indeed has a value property, which you can read and write.

                    Comment

                    Working...