Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery, use ~ as a part of id - how?

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>
like image 937
Ibolit Avatar asked Nov 22 '11 11:11

Ibolit


People also ask

Can I use id in jQuery?

id is not a valid jquery function. You need to use the . attr() function to access attributes an element possesses. You can use .

How can I get the ID of an element using jQuery?

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.

How do you select an item with ID student in JavaScript?

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.

What is $() in jQuery?

$() = 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.


1 Answers

try this

  $("#a\\~b\\~c").change(function(){
    alert('a');
  });
like image 146
erimerturk Avatar answered Oct 01 '22 13:10

erimerturk