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

Related posts:

  1. get CKEditor data with Jquery – no plugin required If you are using the CKeditor also with JQuery (not...
  2. Image upload with AS3 mac problem fix I hit my head several times on the wall before...
  3. Getting the page and viewport dimensions using jQuery Getting the page and viewport dimensions using jQuery is as...
  4. Custom info windows with jQuery and Google Maps Ben Nolan has a writeup on a new feature in...
  5. 25 jQuery image galleries and slideshow plugins For some sites, image galleries are an absolute must. Portfolios...

, , , ,

This post was written by:

- who has written 193 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

14 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.

  8. Banning Stuckey Says:

    Try this
    $(“select[id*=upgrade]“).each(function() {
    $(this).val(“-”);
    });

  9. Banning Stuckey Says:

    ugh sorry i messed up forgot to put upgrade in quotes here

    $(‘select[id*="upgrade"]‘).each(function() {
    $(this).val(“-”);
    });

  10. Karl Rixon Says:

    Note that you don’t actually need the .each in this case. The following works the same:

    $(“select[id^=upgrade]“).val(“-”);

  11. ropox Says:

    @Karl Rixon

    indeed just found my self the other day… thanks for the notice :)


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