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/Bookmark

Related posts:

  1. Custom info windows with jQuery and Google Maps Ben Nolan has a writeup on a new feature in...

, , , ,

8 Responses to “JQuery wildcard selectors”

  1. Dimskiy Says:

    ropox

    Wow! Thank you so much for this information. Your [id^=blah] works like a charm.

    I also want to point out, I had a very similar problem to solve. I also went around the Net to search for a wild card selector.

    \\S* – doesn’t work for me either.

  2. ropox Says:

    well i’m glad I helped someone on this task, cause really I was banging my head to figure out what I was doing wrong : )
    At least i know the \\S* wildcard is not working on someone else either..

  3. Ross Hawkins Says:

    Another “me too” for \\S* not working..

    I’m semi curious to know what it is that we’ve all missed, but it’s hard to justify spending much time on it when the method you’ve posted above works fine :)

  4. Michal Svab Says:

    ropox: Thanks for this tip. I was starting to get crazy with all the variations of ‘\\S*’ I tried and none worked.

  5. Wytze Says:

    There is a lot you can do to select the required items you want to manipulate.

    Have a look at the docs to view all possibilities: http://docs.jquery.com/Selectors

    In my case I needed the: input[name$='letter'] to select the required items because I use JSF to generate the pages.

  6. Campo Says:

    Excellent post, thanks so much for the info, it helped a lot!!

  7. Søren Sprogø Says:

    Try the id$=upgrade instead. $= seems to be the wildcard selector.


Trackbacks/Pingbacks

  1. [...] Comment! UPDATE: The below method no longer works! Go here to see a tested, working method: http://ropox.net/archives/1081 [...]

Leave a Reply