today I come up to a strange problem I couldn’t find a solution for. I have a div which has a hash in its id. Normally I fetch elements with jQuery using
Seriously, though. The valid values for id attributes are letters, numbers (though they can’t START with numbers), dashes and underscores. A hash/pound sign is not a valid value.
OK, technically, anything but white spaces are allowed, but you have to escape them, which is a right royal pain…
You can do it like this using the attribute selector instead:
console.log($("[id='test#test']").attr("id"));
Html5 does technically allow anything except whitespace for an ID but it can be a headache. (CSS has some different rules and won’t recognise certain characters.)
The attribute selector trick gets around those issues.