1

here's what I mean:

I have a few controls, for example,

<asp:DropDownList ID="ddlDate1" runat="server" />
<asp:DropDownList ID="ddlDate2" runat="server" />
<asp:DropDownList ID="ddlDate3" runat="server" />

in my javascript, I want to do something like this:

for (i = 1; i <= count; i++) {
    something[i] = document.getElementById("<%= ddlDateRange" + i + ".ClientID %>");
}

any way I can make this work, or any alternatives?

2 Answers 2

1

Put your dropdown lists into an array. Then you can populate a javascript array like so

var dropDownListIds = [];
<% foreach (DropDownList ddl in myListOfDropDowns) { %>
dropDownListIds.push('<%= ddl.ClientID %>');
<% } %>
Sign up to request clarification or add additional context in comments.

Comments

1

Why not use jQuery and just do something like:

<asp:DropDownList CssClass="ddl" ID="ddlDate1" runat="server" />
<asp:DropDownList CssClass="ddl" ID="ddlDate2" runat="server" />
<asp:DropDownList CssClass="ddl" ID="ddlDate3" runat="server" />

$('.ddl').each(function(){
    var ddl = $(this);
});

4 Comments

You can call just $('select')
@abatishchev, you'd have to assume there aren't any other DropDownLists on the page that he doesn't want included.
Another option: $('#someDiv > select')
@abatishchev, I like that approach and it's probably how i'd have done it if it were my code. Another would be $("select [id*='ddlDate']")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.