The difference between '$ (<% = DDL.ID%>) & $ (' <% = DDL.ID%> ')

I tried to associate an event with a drop-down list from yesterday. Nothing helped in JavaScript chat. No one could find a fix for my problem, but then I somehow tried to link my drop-down list as follows

 $('<%= ddl.ID %>').bind('change',myfunction); 

to

 $(<%= ddl.ID %>).bind('change',myfunction); 

and he started to work, any idea?

EDIT

Sorry, but not working on IE :(

+1
source share
4 answers

jQuery uses CSS selector syntax.

For ID Selector it must be a prefix with the # symbol. Example, $('#DOMElementId')

Check jQuery selector list

In your case, the correct one should be

 $('#<%= ddl.ID %>').bind('change',myfunction) // if you have no master page or //ClientIDMode="static" 

or

 $('#<%= ddl.ClientID %>').bind('change',myfunction) 
+6
source

The jQuery function needs a string as a selector.

The last syntax doesn't matter except

  • if your string is a javascript variable (e.g. document.body or myvar if you defined myvar ).
  • if your ddl.ID string contains quotation marks (perhaps you created it as ddl.ID = "\"#id\""; )

You may have fixed the error with another error that didn’t work that line in your code.

+4
source

You should use ClientID instead

$('#<%= ddl.ClientID %>').bind('change', myfunction);

+2
source

the best way

 $('#'+'<%= ddl.ClientID %>').bind('change',myfunction); 

it works even if you use this control on the user management page or on the content page

 $('#'+'<%= ddl.ClientID %>').change(myfunction); 
+1
source

All Articles