Thursday, 20 May 2010

Reliably reference a .NET control in javascript using jQuery

The clientside id of a .NET control should never be hardcoded into javascript as it's so unpredictable, and is liable to change. There is one predictable thing about it though, and that is that the rendered id will end in '_myOriginalId'.

You can take advantage of this predictability by using this syntax with jQuery, which will reliably find your control.

$("[id$='_myOriginalId']")

No comments:

Post a Comment

Comments are moderated, so you'll have to wait a little bit before they appear!