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>
id is not a valid jquery function. You need to use the . attr() function to access attributes an element possesses. You can use .
The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.
Approach: Use jQuery [attribute^=value] Selector to select the element with ID starts with certain characters. Example 1: This example selects that element whose ID starts with 'GFG' and change their background color.
$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.
try this
$("#a\\~b\\~c").change(function(){
alert('a');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With