JQuery wildcard selectors

Mon, Jan 19, 2009

Programming, Snippets

Ok i spend a lot of time on finding how to work this around using JQuery, but i finally found a solution.

Say we have some elements in our page that we don’t know their ID’s. For example in my case I was printing dynamically select elements but did not know their ID cause each id had an integer to distinguish it from the others. What i wanted to do was to reset all select boxes with a certain word in their ID’s to their first element as soon as one of them changed.

For example all the elements that i wanted to change had the word “upgrade” in them plus some other numbers. What I did was on the onChange function that would trigger the event:

 
$("select[id^=upgrade]").each(function() {
$(this).val("-");
});

which would return to me all elements that started with the word upgrade.  JQuery selectors are great. Using the each function you can go around all selectors and set the value of each select box to “-” which in my case the “-” was the first element of each select box. Usually you will have as the first element something like “0″ or “” (empty).

Also searching I found this interesting snippet which didnt work for me, but i assume i did something wrong.

 $('#myElement').html();

You can use \\S* to make wildcard selections, I.E.:

 $('#myEle\\S*').each();

Dont know why it didnt work on my case….

Share
, , , ,

This post was written by:

- who has written 196 posts on ropox.net.

Software Engineer and Web Developer, keen on new technologies and has passion for web engineering.... Internet is my job and i love it...!!! Founded Baldpixel (baldpixel.com) on 2008 which is specialized on enginnering custom web projects while cooperating with top advertising agencies on the net.

Contact the author