Somebody
Somebody

Reputation: 2779

document.getElementById() returns NULL getting value from DropDownList

I have this code:

<td style="text-align: center;">
            <asp:DropDownList ID="ddlOpRep" runat="server" Width="70px">
                <asp:ListItem Value="1">1</asp:ListItem>
                <asp:ListItem Value="2">2</asp:ListItem>
                <asp:ListItem Value="3">3</asp:ListItem>
            </asp:DropDownList>
        </td>

Now I want to show a confirmation msg with the selected value in the ddlOpRep:

<asp:Button ID="btnRelease" runat="server" Text="Release" Width="130px" 
         OnClientClick="return confirmRelease();" onclick="btnRelease_Click" />

<script type="text/javascript">
    function confirmRelease() {
        var OpRep = document.getElementById("ddlOpRep");           

        return confirm('Are you sure you want to release this configuration: OpRep: ' + OpRep + ' ?');
    }
</script>

But I'm getting null at var OpRep = document.getElementById("ddlOpRep");

So not even the possibility to get var OpRep = document.getElementById("ddlOpRep").value;

Upvotes: 1

Views: 4349

Answers (3)

Use the ClientID to get the element because you are using ASP.NET. It should look something like this:

var newObject = document.getElementById("<%= ASPObject.ClientID %>");

where ASPObject is the ASP.NET object you are looking for.

Upvotes: 0

Sachin
Sachin

Reputation: 40970

In javascript function you need to pass the actual generated Id of the dropdown. So Either you can get the client id like this

document.getElementById("<%= ddlOpRep.ClientID %>");

or You can set the ClientIdMode="Static" in dropdown property.

<asp:DropDownList ID="ddlOpRep" runat="server" Width="70px" ClientIdMode="Static">

Upvotes: 2

tymeJV
tymeJV

Reputation: 104775

It's ASP.NET, get the ClientID

var OpRep = document.getElementById("<%= ddlOpRep.ClientID %>");

Upvotes: 2

Related Questions