Ventricle Ventricle - 7 months ago 13
Javascript Question

jQuery - select element starting and ending with... but omit specific name

I have elements generated on my board that have IDs like so "test_***_fast". The 3 stars are random numbers generated by the site.

Problem is that I want to write an action for all IDs except for test_1_fast, because this one is static and important.

I know I can't use:

$('[id^=test][id$=_fast]')


because it works for test_1_fast as well and code blow obviously doesn't work :D

$('[id^=test][id$=_fast]' - '#test_1_fast')


So how should I put this to make it work?

Answer

Use :not() for avoiding certain element

$('[id^=test][id$=_fast]:not(#test_1_fast)').css('color', 'red')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


<span id="test_0_fast">a</span>
<span id="test_1_fast">a</span>
<span id="test_2_fast">a</span>


or not()

$('[id^=test][id$=_fast]').not('#test_1_fast').css('color', 'red')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>


<span id="test_0_fast">a</span>
<span id="test_1_fast">a</span>
<span id="test_2_fast">a</span>

Comments