0

Basically I'd like to close all closeclass[id].

<div class="closeclass4"></div>
<div class="closeclass15"></div>
<div class="closeclass12"></div>
<div class="closeclass10"></div>
$( document ).on( "touchstart", ".comments", function(e) {
  $(".closeclass").hide(); // closeclass always has a number after it
});

is it possible?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
RGS
  • 4,062
  • 4
  • 31
  • 67
  • 3
    Use $("[id^=closeclass]") for all classes beginning with "closeclass". See this answer: https://stackoverflow.com/questions/5376431/wildcards-in-jquery-selectors – Michael Beeson Oct 21 '18 at 11:26
  • Possible duplicate of [jquery selector for id starts with specific text](https://stackoverflow.com/questions/23223526/jquery-selector-for-id-starts-with-specific-text) – Mohammad Oct 21 '18 at 11:28
  • 1
    it is not starting, it is ending. – RGS Oct 21 '18 at 11:29
  • 1
    Give them a common class name in addition to the unique ones then the code shown would work – charlietfl Oct 21 '18 at 11:33
  • 1
    It's right. You need to select divs has class start with `closeclass` – Mohammad Oct 21 '18 at 11:36
  • 3
    Get rid of the numbers after the `closeclass` name as they're redundant. Classes are for grouping elements. Then your problem becomes moot. If you want to identify elements uniquely use `id` or a `data` attribute of some kind. – Rory McCrossan Oct 21 '18 at 11:37

1 Answers1

1

As Michael pointed out in the comments, all you need to do is use the [attribute^=value] selector. Though since you're using classes, we use class^= instead of id^=

$("[class^=closeclass]").css('background', 'red'); 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="closeclass4">test</div>
<div class="closeclass15">test</div>
<div class="foo">test</div>
<div class="closeclass10">test</div>
ProEvilz
  • 5,310
  • 9
  • 44
  • 74
  • 3
    Should stop and figure out why unique classes are used in the first place also – charlietfl Oct 21 '18 at 11:39
  • 1
    I do not disagree with that. – ProEvilz Oct 21 '18 at 11:40
  • 3
    Please note that this will fail if `closeclass` is not listed first, e.g. `
    test
    `. I'd go with `$("[class*=closeclass]")` (even if that has fail cases as well, but probably less common).
    – connexo Oct 21 '18 at 11:40
  • it won't fail in jquery. – skobaljic Oct 21 '18 at 11:41
  • 2
    @skobaljic It will. – connexo Oct 21 '18 at 11:41
  • Yeah but that's a different issue... otherwise, you can use `class~=` - I'm going off the provided code only. – ProEvilz Oct 21 '18 at 11:42
  • Yes, just tried, it fails [here](https://jsfiddle.net/7d9qkcbw/), I'd consider it a bug. – skobaljic Oct 21 '18 at 11:42
  • @skobaljic *= is probably less failprone, but will fail here: https://jsfiddle.net/7d9qkcbw/1/ – connexo Oct 21 '18 at 11:44
  • @connexo OP only asked about how their code is right now. Didn't mention anything to do with extra classes within their elements so it seems somewhat irrelevant – ProEvilz Oct 21 '18 at 11:45
  • Fine, but good to mention for other guys reading this post in the future. – skobaljic Oct 21 '18 at 11:45
  • 1
    @connexo Why have you edited my answer? It's a pointless addition for the reasons I've explained above. – ProEvilz Oct 21 '18 at 11:47
  • It's a good idea to clearly show weaknesses of an approach that way. Even moreso, since you haven't elaborated on the prerequisites your approach expects. – connexo Oct 21 '18 at 11:48
  • @skobaljic that's not a bug at all. It's just reading the entire `className` property value and comparing the starting value. Using an attribute selector on classes (while it may work in this case) is always a very big code smell which should be investigated and avoided where possible. – Rory McCrossan Oct 21 '18 at 11:53
  • @connexo But we go off what OP provided. You're assuming that there **will** be extra classes in their div elements and then you're coming up with a solution for it. OP hasn't specified that there **will** be. Until that is done, you shouldn't assume and alter the original question asked and provide a potential solution for what seems like extra kudos. Especially on someone else's answer. We could do that for a multitude of things and come up with fixes for 'weak approaches' - not just extra classes being potentially used. – ProEvilz Oct 21 '18 at 11:56