This is a migrated thread and some comments may be shown as answers.

Issue creating jQuery selectors with special characters

1 Answer 21 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ryan Haugh
Top achievements
Rank 1
Ryan Haugh asked on 03 Aug 2010, 10:50 PM
I have found a pretty important issue with the way the telerik controls generate their jQuery selectors.  Consider the following example:

Html.Telerik().DatePicker()
            .Name("DatePicker[0]")
            .MinDate(Model.MinDate.Value)
            .MaxDate(Model.MaxDate.Value)
            .Value(Model.SelectedDate.Value)
            .ShowButton(Model.ShowButton.Value)
This would be a very common naming style when dynamically creating a variable list of input rows in MVC such as:

Date 1: <datepicker with ID InputtedDate[0]>
Date 2: <datepicker with ID InputtedDate[1]>
...
Date n <datepicker with ID InputtedDate[n]>

The problem lies in the code that generates the jQuery selector (in this case ClientSideObjectWriter.Start()).  For the above example, the following JavaScript code would be rendered:

jQuery('#DatePicker[0]').tDatePicker({format:'M/d/yyyy', selectedDate:new $.telerik.datetime(2010,07,03), minDate:new $.telerik.datetime(1900,00,01), maxDate:new $.telerik.datetime(2099,11,31)});
This is incorrect because the selector engine would see that as attribute '0' in the element with ID 'DatePicker'.  This needs to be escaped:
jQuery('#DatePicker\\[0\\]').tDatePicker({format:'M/d/yyyy', selectedDate:new $.telerik.datetime(2010,07,03), minDate:new $.telerik.datetime(1900,00,01), maxDate:new $.telerik.datetime(2099,11,31)});

A very simple fix would be to escape all special characters before writing the selector in the Start method:
string selector = @"#;&,.+*~':""!^$[]()=>|/".ToCharArray().Aggregate(id, (current, chr) => current.Replace(chr.ToString(), @"\\" + chr));
 
writer.Write("jQuery('#{0}').{1}(".FormatWith(selector, type));

If I'm missing something, please let me know.

Ryan

1 Answer, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 04 Aug 2010, 08:07 AM
Hello Ryan Haugh,

You are absolutely right. The problem really exists in the current official release. I tried the suggested workaround and it works flawlessly. The problem is now fixed and the fix will appear in the next release.

I have updated your Telerik points.

Regards,
Atanas Korchev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
General Discussions
Asked by
Ryan Haugh
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Share this question
or