2010年12月9日 星期四

[jQuery] Select DIV element by ID

It's a sample code below.

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
  $('div[id*="mydiv"]').append('*');  // containing the a given substring.
  $('div[id|="mydiv"]').append('|');  // mydiv- (string followed by a hyphen (-)
  $('div[id$="mydiv"]').append('$');  // a value ending exactly with a given string.
});

</script>

</script>
<div id="mydiv"></div>
<div id="0mydiv0"></div>
<div id="mydiv-1"></div>
<div id="mydiv1"></div>
<div id="mydiv2"></div>
<div id="3mydiv"></div>


Result:

*$
*
*|
*
*
*$

Reference: http://api.jquery.com/category/selectors/
Related Posts Plugin for WordPress, Blogger...