In my application I have a form, which elements are named using a certain convention, i.e. they are paths, the parts of which are separated using the ~
sign.
Now I need to access one of them in jQuery by id, but I fail. Apparently, jQuery treats it as the #prev ~ sibling
thing.
Is there a way I can sort of escape the ~
sign in the jQuery function?
Here is an example of what my code looks like:
<select id="a~b~c">
<option value='1'>one</opiton>
</select>
<script>
$("#a~b~c").change(function(){
alert('a');
});
</script>
try this
$("#a\\~b\\~c").change(function(){
alert('a');
});
There is an answer to that in the official jQuery FAQ.
You need to escape it with \\
.
You can use \\
http://api.jquery.com/category/selectors/
$('#a\\~b\\~c')
Or, if escaping is problematic you can use: http://api.jquery.com/attribute-equals-selector/
$('[id="a~b~c"]')
Fiddle Demo
The escape character in jQuery is two backslahses, \\
, so try this:
$("#a\\~b\\~c").change(function(){
alert('a');
});
Fiddle to show it working
Further reading on jQuery selectors: http://api.jquery.com/category/selectors/