2

How do you get a drop down list to control the value of a Javascript variable?

I'm trying to give the option of filtering a table by year, and I want this dropdownlist to change the value of a Javascript variable called yearVariable.

My code:

<%=Html.DropDownList("yearVariable", Model.myArray, new { onchange =javascript:ReloadTable()" })%>

(as you can see, the dropdownlist is created using HTML Helper, getting its values from an array; also I don't want to have to click a submit button.) Thanks!

2
  • Are you using any specific javascript framework or just plain javascript? Commented Feb 9, 2012 at 13:14
  • MVC. Actually I've kind of worked it out: <%=Html.DropDownList("yearFilter", Model.PossibleYears, new { onchange = "javascript:ReloadTable(this.value)" })%> Commented Feb 9, 2012 at 13:16

1 Answer 1

3

You are simply just missing a quote in your original code:

<%=Html.DropDownList(
    "yearVariable", 
    Model.myArray, 
    new { onchange = "javascript:ReloadTable()" })%>
Sign up to request clarification or add additional context in comments.

Comments

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.