0

I'm not sure how to handle the issue to get a value of the control if its ID was passed to a function from code behind.

I have a function called StateSelect that is added to a control in code behind. It has parameters. The last parameter is txtCity.ClientID.ToString()

In my html, I have this function defined as

function StateSelectedp(city, state, ddlZipName, txtCityID)
{
    alert($.trim($('#txtCityID').value)); //not sure how to do it

}

I need to get a value of the txtCityID control. How can I reference it with jQuery?

Thank you

dave mankoff
  • 17,379
  • 7
  • 50
  • 64
gene
  • 2,098
  • 7
  • 40
  • 98

1 Answers1

0

To get the value of an element with a variable ID, you can do something along the lines of the following...

function StateSelectedp(city, state, ddlZipName, txtCityID)
{
    alert($('#' + txtCityID).val());
}

txtCityID is a string representing the whole ID of a given element. For jQuery to search by id, the id must be prepended by the '#' character. After the element has been selected, the val() function will return the value of the first (I think it's only first, anyways) element in the selected set of elements. There should only be one anyways, as only one element should have a given id.

Keep in mind, though, that this is all happening on the client side. Your reference to code-behind (a Web Forms term) implies that you might be intending to do something with this on the server-side, so this may or may not be the path you're really looking for.

Ixonal
  • 616
  • 8
  • 18