4

I have an input box generated by

 <%= Html.TextBoxFor(model => model.Card.ExpiryDate) %>

which results in the html

<input id="Card_Expiry_Date" name="Card.ExpiryDate" type="text" value="">

I also have javascript function

<script type="text/javascript">
   $().ready(function() {
   $('#Card_Expiry_Date').datepicker({ dateFormat: 'yy-mm-dd' });
</script>

How can I retrieve the input id that will be generated so that I don't have to hard-code the id in my function?

dnatoli
  • 6,972
  • 9
  • 57
  • 96
  • 1
    http://stackoverflow.com/questions/3065307/client-id-for-property-asp-net-mvc/3069808#3069808 – Ahmad Nov 05 '10 at 08:29

3 Answers3

1

You can specify the ID when creating the textbox:

<%= Html.TextBoxFor(model => model.Card.ExpiryDate, new { id="Card_ExpiryDate" })  %>

Then this will surely work:

 $('#Card_ExpiryDate')...
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
1

Thanks Ahmad for you comment, that was what I was looking for!

Client Id for Property (ASP.Net MVC)

Community
  • 1
  • 1
dnatoli
  • 6,972
  • 9
  • 57
  • 96
0

I'm not sure for MVC, but in Forms you would inject the textbox id into the html. Something akin to:

$get('<%= TextBox1.ClientID %>')

Link with explanation here.

Daniel Szabo
  • 7,181
  • 6
  • 48
  • 65