how to get <option> id

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • eihabisaac
    New Member
    • Aug 2007
    • 38

    how to get <option> id

    hey every body

    how can i get using javascript the selected option <option> ID or Value because when ever i clicked it i get the <select> id and i dont want that

    [HTML]<select name="list" id="list" size="10%" >
    <option id="3" ondblclick="sel ect_song();">Ap ple
    <br> </option>
    <option id="4" ondblclick="sel ect_song();">Or ange
    <br> </option>
    <option id="5" ondblclick="sel ect_song();">Ba nana<br> </option>
    <option id="6" ondblclick="sel ect_song();">le mon<br> </option>
    </select>[/HTML]

    Code:
    function select_song()
    {
    	var i = event.srcElement.id
                   alert(i); // this give me an alert (list)
    
    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Why not just go through a Javascript tutorial to find out how to use a select?

    Comment

    • eihabisaac
      New Member
      • Aug 2007
      • 38

      #3
      i can teach you how to use javascript and if you dont know the answer plz dont replay

      and dont be disrespectial cuz u r an admin

      thanks anyway

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by eihabisaac
        i can teach you how to use javascript and if you dont know the answer plz dont replay

        and dont be disrespectial cuz u r an admin

        thanks anyway
        It's not that I want to be disrespectful.
        We just have some rules on this forum which unfortunately I have to enforce.
        Giving you the code makes your current problem go away but reading a tutorial makes future potential problems go away as well. There are also lots of threads in this forum which can give you the answer, but I still prefer that you read a Javascript tutorial on how to use a select.

        Reading, unfortunately has become a lost art.

        Comment

        • hsriat
          Recognized Expert Top Contributor
          • Jan 2008
          • 1653

          #5
          I guess it too hot these days.. month of July approaching.. I hate it. I have ordered some soft drinks for you guys.

          While we are waiting for the drinks, let me look into the JavaScript problem.
          [code=javascript]var list = event.srcElemen t;
          alert(list.opti ons[list.selectedIn dex].id);[/code]

          I didn't test it. But I think it should work.

          You may also read this tutorial.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by hsriat
            I guess it too hot these days.. month of July approaching.. I hate it. I have ordered some soft drinks for you guys.

            While we are waiting for the drinks, let me look into the JavaScript problem.
            [code=javascript]var list = event.srcElemen t;
            alert(list.opti ons[list.selectedIn dex].id);[/code]

            I didn't test it. But I think it should work.

            You may also read this tutorial.
            July is winter for us.

            P.S You just gave someone fish instead of teaching them how to fish. Better stick around in case they run out of the fish that you gave them.

            Comment

            • hsriat
              Recognized Expert Top Contributor
              • Jan 2008
              • 1653

              #7
              Originally posted by r035198x
              July is winter for us.

              P.S You just gave someone fish instead of teaching them how to fish. Better stick around in case they run out of the fish that you gave them.
              umm.. then you must be in Aus or NZ I guess. Or somewhere in Southern Hemisphere.

              And I linked a small tutorial too to let him know how I fish.

              Regards,
              Harpreet [ doen't not really intend to hurt anyone :) ]

              Comment

              • acoder
                Recognized Expert MVP
                • Nov 2006
                • 16032

                #8
                srcElement is IE-only. target is the standard, so you'll have to use a combination to support all browsers.

                One more problem: IDs shouldn't begin with numbers.

                Note also that IE treats the options as part of the select (can be annoying), but not all browsers do.

                Comment

                • eihabisaac
                  New Member
                  • Aug 2007
                  • 38

                  #9
                  Originally posted by hsriat
                  I guess it too hot these days.. month of July approaching.. I hate it. I have ordered some soft drinks for you guys.

                  While we are waiting for the drinks, let me look into the JavaScript problem.
                  [code=javascript]var list = event.srcElemen t;
                  alert(list.opti ons[list.selectedIn dex].id);[/code]

                  I didn't test it. But I think it should work.

                  You may also read this tutorial.
                  thanks hsriat

                  it works and thanks for the tutorial ,,,,

                  Comment

                  • hsriat
                    Recognized Expert Top Contributor
                    • Jan 2008
                    • 1653

                    #10
                    Originally posted by eihabisaac
                    thanks hsriat

                    it works and thanks for the tutorial ,,,,
                    As acoder said, srcElement is an IE thing. So you better call that function with an argument which refers to the select box.
                    selectSong(this .parentNode)

                    Comment

                    Working...