1

$("#slide") code formating i've got something like this, i want my function to select all id's starting from slide, slide1, slide2 etc.

In bash there is slide*, which does that job, is there anything like this in jquery?

Thank you for your help

Santosh Ram Kunjir
  • 1,074
  • 1
  • 12
  • 23
Miyagi
  • 127
  • 8
  • 1
    Possible duplicate of [jQuery selector regular expressions](http://stackoverflow.com/questions/190253/jquery-selector-regular-expressions) – jtabuloc Feb 12 '17 at 02:36
  • There is no wildcards in selectors. But there is a lot of **CSS Selectors** that let you acheive pretty much the same results. Here is a [**list**](http://www.w3schools.com/cssref/css_selectors.asp) of all of them! – ibrahim mahrir Feb 12 '17 at 03:54

4 Answers4

4

You can use the attribute selector in combination with :not() to select slide* and exclude #slide2

$('[id^="slide"]:not("#slide2")').css('color','red');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="slide">slide</div>
<div id="slide1">slide1</div>
<div id="slide2">slide2</div>
<div id="slide3">slide3</div>
<div id="slide4">slide4</div>
Michael Coker
  • 52,626
  • 5
  • 64
  • 64
  • Thank you very much – Miyagi Feb 12 '17 at 02:33
  • Is there any way to select all slide except one? for example select slide, slide1, slide3, etc with ommiting slide2? – Miyagi Feb 12 '17 at 02:35
  • Well that depends, I understood the OP wants to be select all elements where the id starts with the string "slide", hence my answer. @Miyagi, please clarify! Even though you thanked for this answer already, it might eventually just work because all your slides also contain the word "slide" anywhere, but the approach would still be wrong. Bit it's hard to tell without knowing your markup. – Constantin Groß Feb 12 '17 at 02:38
  • @Miyagi it's unclear what you're looking for. Do you want to find divs that are `id="slide1"`, `id="slide3"`, etc, or are you looking for a div that has `slide1` as text in the div? – Michael Coker Feb 12 '17 at 02:39
  • It is working :) can I add an exception for slide2? how to do this? – Miyagi Feb 12 '17 at 02:39
  • I want to find divs with id="slide1" id="slide3" id="slide4" etc – Miyagi Feb 12 '17 at 02:40
3

yes $('[id^="slide"]'). This says: any Node that has an id-attribute that starts with "slide";

But your actual mistake is to use IDs in this place. Enumerated whatever hint that you're actually dealing with a list of some kind. In this case this list would better be represented by a (css) class instead of a set of IDs.

*Enumerated whatever means: enumerated variables, properties, methods, functions, IDs or classes, ... In each of these cases you should determine the nature of this group and reconsider your structure.

Thomas
  • 11,958
  • 1
  • 14
  • 23
2
  1. jQuery is not necessary
  2. In this case it's better to use classes, or element tags, in place of IDs

Given what you have, you could easily look at the start of the id attribute for "slide" and collect all except slide2 using the :not() selector filter function.

let slides = document.querySelectorAll('[id^="slide"]:not(#slide2)');
console.log(slides);

slides.forEach(s=>s.innerHTML+=' - matched');
<div id="slid1">not slide</div>
<div id="slide">slide</div>
<div id="slide1">slide1</div>
<div id="slide2">slide2</div>
<div id="slide3">slide3</div>
<div id="slid2">not slide</div>
vol7ron
  • 40,809
  • 21
  • 119
  • 172
0
$('[id^="slide"]').not('#slide2');

Is what you are looking for as a css selector.

Constantin Groß
  • 10,719
  • 4
  • 24
  • 50