Javascript Wildcard with jQuery

Posted on the October 1st, 2008 under Javascript by admin

One of the developers I work with came up with a handy bit of JavaScript that uses a wildcard to look for a div that has “div-comment-” in its ID. It then does a display none on them. The handy thing about this basic bit of script is that it can be used across a website to edit the css.

Remember for this to work you will need jquery to be called in within the head of your page. This can be done be done by downloading jquery from here and using code like this in your page head…

<script language=”JavaScript” type=”text/javascript” src=”https://yourwebsite.com/jquery-1.2.6.js”></script>

Example Use

Imagine you have a series of div’s that are displayed on a page such as comment boxes. Those div’s have ID’s of div-comment-1, div-comment-2, div-comment-3 and so on. You want to click

So here’s the JavaScript you put in the head of the page.

<script>
function MyFunction() {
$(”div[id*='div-comment-']“).css(’display’, ‘none’);
}
</script>

And the link will look like this..

<a href=”javascript: MyFunction();”>

That link could be in a text format or in the form of a button/image.

Essentially this handy bit of JavaScript can be used endlessly across a website to give the user more control over the look and appearance of the page.

Share this Solution:
  • Digg
  • StumbleUpon
  • del.icio.us
  • Technorati
  • Google Bookmarks
  • Facebook
  • Live
  • Sphinn
  • Furl

5 Responses to 'Javascript Wildcard with jQuery'

Subscribe to comments with RSS
  1. jimmy said, on October 2nd, 2008 at 7:29 pm

    I just used this bit of javascript on my site and it saved me a load of trouble! cheers!

  2. redsquare said, on October 3rd, 2008 at 5:08 am

    why jQuery 1.1.3? There is 1.2.6 available!!

  3. admin said, on October 6th, 2008 at 9:29 am

    Duly noted redsquare, post updated.

    Thanks for the comment!

  4. B said, on December 10th, 2008 at 2:15 pm

    I was trying to get this type of wildcard selector to work for about five hours straight! I appreciate you posting very very much!

    I was trying to make a selector such as $(”#div-comment-*”) and even using custom-made regexs to get it to work.

    Thank you, thank you, thank you!

  5. Jimmy said, on August 11th, 2009 at 2:10 pm

    I realised after hours today that this doesn’t work for duplicated ID’s – just prefix or suffix – should have guessed really! doh!

Leave a Reply
XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>