Calling Javascript on a Dropdown list

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?Qw==?=

    Calling Javascript on a Dropdown list

    I have a dropdown list as below.

    I add an onchnage attribute in my codebehind to call some Javascript.

    I want to get the selected text from my dropdown. What am I doing wrong below?

    <asp:DropDownLi st ID="ddlStatus" runat="server" />

    In my codebehind

    ddlStatus.Attri butes["onchange"] = "javascript:Set DateFields();";



    <script language="javas cript" type="text/javascript">

    function SetDateFields()

    {

    var statusList = document.getEle mentById("ddlSt atus");


    var selectedStatus = statusList.opti ons[statusList.sele ctedIndex].value;


    }

    </script>

  • =?UTF-8?B?R8O2cmFuIEFuZGVyc3Nvbg==?=

    #2
    Re: Calling Javascript on a Dropdown list

    C wrote:
    I have a dropdown list as below.
    >
    I add an onchnage attribute in my codebehind to call some Javascript.
    >
    I want to get the selected text from my dropdown. What am I doing wrong below?
    Why do you think that you are doing something wrong? What happens when
    you try to use the code? Do you get any error message? Have you enabled
    Javascript error messages in the browser?
    <asp:DropDownLi st ID="ddlStatus" runat="server" />
    >
    In my codebehind
    >
    ddlStatus.Attri butes["onchange"] = "javascript:Set DateFields();";
    The "javascript :" protocol is only used when you put Javascript in an
    URL. The only reason that you don't get an error message when you use it
    elsewhere, is that it becomes a label instead.

    ddlStatus.Attri butes["onchange"] = "SetDateFields( );";
    <script language="javas cript" type="text/javascript">
    >
    function SetDateFields()
    >
    {
    >
    var statusList = document.getEle mentById("ddlSt atus");
    Have you checked that this is the actual id that the element gets in the
    html code?
    var selectedStatus = statusList.opti ons[statusList.sele ctedIndex].value;
    You are getting the value instead of the text.
    }
    >
    </script>
    >

    --
    Göran Andersson
    _____
    Göran Anderssons privata hemsida.

    Comment

    • Laurent Bugnion [MVP]

      #3
      Re: Calling Javascript on a Dropdown list

      Hi,

      C wrote:

      <snip>
      <script language="javas cript" type="text/javascript">
      Additionally to Göran's remarks, the "language" attribute is deprecated.
      You can safely remove it and use only the "type" attribute.

      Greetings,
      Laurent
      --
      Laurent Bugnion [MVP ASP.NET]
      Software engineering: http://www.galasoft-LB.ch
      PhotoAlbum: http://www.galasoft-LB.ch/pictures
      Support children in Calcutta: http://www.calcutta-espoir.ch

      Comment

      • =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=

        #4
        RE: Calling Javascript on a Dropdown list

        As mentioned on the other forum, aside from the issues adressed by the
        others, you are doing anything wrong. Try putting alert(selectedS tatus) just
        before the closing brace and you will see the value.
        Peter

        --
        Site: http://www.eggheadcafe.com
        UnBlog: http://petesbloggerama.blogspot.com
        Short urls & more: http://ittyurl.net




        "C" wrote:
        I have a dropdown list as below.
        >
        I add an onchnage attribute in my codebehind to call some Javascript.
        >
        I want to get the selected text from my dropdown. What am I doing wrong below?
        >
        <asp:DropDownLi st ID="ddlStatus" runat="server" />
        >
        In my codebehind
        >
        ddlStatus.Attri butes["onchange"] = "javascript:Set DateFields();";
        >
        >
        >
        <script language="javas cript" type="text/javascript">
        >
        function SetDateFields()
        >
        {
        >
        var statusList = document.getEle mentById("ddlSt atus");
        >
        >
        var selectedStatus = statusList.opti ons[statusList.sele ctedIndex].value;
        >
        >
        }
        >
        </script>
        >

        Comment

        • =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=

          #5
          RE: Calling Javascript on a Dropdown list

          Hi there,

          In addition to Goran's reply, please find working snippet below:

          -- begin aspx code --

          <asp:DropDownLi st ID="ddlStatus" runat="server"
          AutoPostBack="f alse" OnChange="SetDa teFields()">
          <asp:ListItem Text="Status1" Value="Value1"/>
          <asp:ListItem Text="Status2" Value="Value2"/>
          <asp:ListItem Text="Status3" Value="Value3"/>
          </asp:DropDownLis t>

          <script type="text/javascript">
          function SetDateFields()
          {
          var statusList = document.getEle mentById('<%=dd lStatus.ClientI D %>');
          var selectedStatus = statusList.opti ons[statusList.sele ctedIndex].text;

          alert('selected status is : ' + selectedStatus) ;
          }
          </script>
          -- end aspx code --

          Note you have to use control.ClientI D to get the valid ID (ClientID contains
          all the parent controls ids - if i didnt use it and moved dropdown list to a
          Panel control it'd stop work). You may be wondering why i put OnChange
          attribute to drop down list declaration. This is allowed (even if you get a
          warning from schema validation) and it's called 'expando'.

          Hope it's clear now


          Milosz


          "C" wrote:
          I have a dropdown list as below.
          >
          I add an onchnage attribute in my codebehind to call some Javascript.
          >
          I want to get the selected text from my dropdown. What am I doing wrong below?
          >
          <asp:DropDownLi st ID="ddlStatus" runat="server" />
          >
          In my codebehind
          >
          ddlStatus.Attri butes["onchange"] = "javascript:Set DateFields();";
          >
          >
          >
          <script language="javas cript" type="text/javascript">
          >
          function SetDateFields()
          >
          {
          >
          var statusList = document.getEle mentById("ddlSt atus");
          >
          >
          var selectedStatus = statusList.opti ons[statusList.sele ctedIndex].value;
          >
          >
          }
          >
          </script>
          >

          Comment

          • =?Utf-8?B?TWlsb3N6IFNrYWxlY2tpIFtNQ0FEXQ==?=

            #6
            RE: Calling Javascript on a Dropdown list

            Hi there,

            In addition to Goran's reply, please find working snippet below:

            -- begin aspx code --

            <asp:DropDownLi st ID="ddlStatus" runat="server"
            AutoPostBack="f alse" OnChange="SetDa teFields()">
            <asp:ListItem Text="Status1" Value="Value1"/>
            <asp:ListItem Text="Status2" Value="Value2"/>
            <asp:ListItem Text="Status3" Value="Value3"/>
            </asp:DropDownLis t>

            <script type="text/javascript">
            function SetDateFields()
            {
            var statusList = document.getEle mentById('<%=dd lStatus.ClientI D %>');
            var selectedStatus = statusList.opti ons[statusList.sele ctedIndex].text;

            alert('selected status is : ' + selectedStatus) ;
            }
            </script>
            -- end aspx code --

            Note you have to use control.ClientI D to get the valid ID (ClientID contains
            all the parent controls ids - if i didnt use it and moved dropdown list to a
            Panel control it'd stop work). You may be wondering why i put OnChange
            attribute to drop down list declaration. This is allowed (even if you get a
            warning from schema validation) and it's called 'expando'.

            Hope it's clear now


            Milosz


            "C" wrote:
            I have a dropdown list as below.
            >
            I add an onchnage attribute in my codebehind to call some Javascript.
            >
            I want to get the selected text from my dropdown. What am I doing wrong below?
            >
            <asp:DropDownLi st ID="ddlStatus" runat="server" />
            >
            In my codebehind
            >
            ddlStatus.Attri butes["onchange"] = "javascript:Set DateFields();";
            >
            >
            >
            <script language="javas cript" type="text/javascript">
            >
            function SetDateFields()
            >
            {
            >
            var statusList = document.getEle mentById("ddlSt atus");
            >
            >
            var selectedStatus = statusList.opti ons[statusList.sele ctedIndex].value;
            >
            >
            }
            >
            </script>
            >

            Comment

            • Bhaskardeep Khaund

              #7
              Re: Calling Javascript on a Dropdown list

              Hi,

              U can acheive that by writing

              ddlStatus.Attri butes.Add("onch ange","javascri pt:SetDateField s()")


              Bhaskar

              "C" <C@discussions. microsoft.comwr ote in message
              news:59EC5CBB-DCB5-478E-A8CD-130CF901B036@mi crosoft.com...
              >I have a dropdown list as below.
              >
              I add an onchnage attribute in my codebehind to call some Javascript.
              >
              I want to get the selected text from my dropdown. What am I doing wrong
              below?
              >
              <asp:DropDownLi st ID="ddlStatus" runat="server" />
              >
              In my codebehind
              >
              ddlStatus.Attri butes["onchange"] = "javascript:Set DateFields();";
              >
              >
              >
              <script language="javas cript" type="text/javascript">
              >
              function SetDateFields()
              >
              {
              >
              var statusList = document.getEle mentById("ddlSt atus");
              >
              >
              var selectedStatus = statusList.opti ons[statusList.sele ctedIndex].value;
              >
              >
              }
              >
              </script>
              >

              Comment

              • Laurent Bugnion [MVP]

                #8
                Re: Calling Javascript on a Dropdown list

                Hi,

                Bhaskardeep Khaund wrote:
                Hi,
                >
                U can acheive that by writing
                >
                ddlStatus.Attri butes.Add("onch ange","javascri pt:SetDateField s()")
                This is the same as what the OP did

                ddlStatus.Attri butes["onchange"] = "javascript:Set DateFields();";

                Additionally, had you read previous answers, you would have seen that
                you should never use "javascript :", except in very very specific cases.

                HTH,
                Laurent
                --
                Laurent Bugnion [MVP ASP.NET]
                Software engineering: http://www.galasoft-LB.ch
                PhotoAlbum: http://www.galasoft-LB.ch/pictures
                Support children in Calcutta: http://www.calcutta-espoir.ch

                Comment

                Working...