0

I have a button which when clicked should just pass the selected value from dropdownlist to a textbox.Here is the code :

    <td>
    <script type="text/javascript">
    function calculateCity() {

     var city = document.getElementById('<%= ddlCity.ClientID%>');
     txtCity0.value = city.options[city.selectedIndex].value;
                }
     </script>

     <asp:TextBox ID="txtCity0" runat="server" Width="260px"></asp:TextBox>
     <asp:DropDownList ID="ddlCity" runat="server">
     <asp:ListItem Value="1">Mumbai</asp:ListItem>
     <asp:ListItem Value="2">Pune</asp:ListItem>
     </asp:DropDownList>
     <input id="btnCity" onclick="calculateCity();" type="button" value="Calculate City" /></td>

This code just doesnt do anything.Any help will be appreciated.Thank You

3
  • This is totally unrelated to your question, but out of curiosity. Why are you keeping the <script> tag inside <td>? Is there any special requirement? I'd like to know about that. Commented Mar 21, 2015 at 18:20
  • No logic as such.I thought the script runs inside the same td where i am using it? Commented Mar 21, 2015 at 18:24
  • So, just to let you know, when you call document.AnyXYZfunction(), it works on the whole document. It is better to keep only one script tag for whole page, and keep it in the head. If you are working on a Content page then keep it on top or bottom of the page. Commented Mar 21, 2015 at 18:32

2 Answers 2

1

You are finding the drop down correctly, but then not finding the text box and simply trying to use it.

You need a similar getElementById line for the text box

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:-

function calculateCity() {
    var city = document.getElementById('<%= ddlCity.ClientID%>');
    document.getElementById('<%= txtCity0.ClientID%>').value = 
                                                city.options[city.selectedIndex].value;
 }

Or if you can use jQuery, then you can do this:-

$("#<%= btnCity.ClientID %>").click(function() {
    $('#<%= txtCity0.ClientID %>').val($('#<%= ddlCity.ClientID %>').val());
});

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.