How to use wildcards like $ ('#name*'), $ ('#name%') in jQuery selectors?



For getting the id that begins or ends with a particular string in jQuery selectors, you shouldn’t use the wildcards $('#name*'), $('#name%'). Instead use the characters ^and $. The ^ is used is used to get all elements starting with a particular string. The $ is used is used to get all elements ending with a particular string.

Example

You can try to run the following code to learn how to get id beginning and endingwith a particular string.

Live Demo

<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){   $("[id$=new1]").css("background-color", "yellow");   $("[id^=myid]").css("background-color", "green"); }); </script> </head> <body> <div id="idnew1" myattr="javasubject">Java</div> <div id="idnew2" myattr="htmlsubject">HTML</div> <div id="myid1" myattr="rubysubject">Ruby</div> <div id="myid2" myattr="cppsubject">C++</div> </body> </html>
Updated on: 2020-06-13T12:27:36+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements