1

I work in application on symfony 1.4. And I have some widget like slidebar to change some value. I try to get the value in javascript and I see it contain in div and the div has this kind of id "professionnal_competence_rayon_competence3_slider_value". But the number change because I concatenate with an id from my database.

So I try to get all the div who contain the id like my exemple unsing regex. I use the querySelectorAll but it's not seem working. But if someone has an other way to use regex for my case I'm open.

This an exemple I try to use with regex:

var elements2 =  document.querySelectorAll("[id='/professionnal_competence_rayon_competence[0-9]_slider_value/']");
        console.log(elements2);

I hope it's good but I can explain if you don't understand. But I tru everything and I don't found the solution. So someone know how I can use regex to get a group of div using by id ? Thank in advance!!

Arendelle
  • 117
  • 1
  • 10

1 Answers1

2

You need to use

var elements2 = document.querySelectorAll('[id^="professionnal_competence_rayon_competence"]');

This is the attribute starts with selector which means that id starts with professionnal_competence_rayon_competence and you do not necessarily check for the numeric part here.

Ankit Agarwal
  • 30,378
  • 5
  • 37
  • 62
  • 1
    Unfornately I already try and it give too lot result. I really want to get this type of id: "professionnal_competence_rayon_competence[0-9]_slider_value". Because some other element begin by "professionnal_competence_rayon_competence" and I don't want them. – Arendelle Jun 12 '18 at 13:29
  • Sorry for the double post but have you an idea ? It's impostant. – Arendelle Jun 12 '18 at 13:37
  • @Arendelle You can filter the elements that you got from `document.querySelectorAll` via javascript. I would like to provide an answer, but your question has already been marked as a duplicate :/ – schroffl Jun 12 '18 at 13:43
  • 1
    @Arendelle I created an [example on jsfiddle](http://jsfiddle.net/13afezn7/10/) which marks the matching elements in orange. I hope you get the idea :) – schroffl Jun 12 '18 at 13:58
  • Yes thank you for your help !! And sorry to create a duplicata. – Arendelle Jun 12 '18 at 14:35
  • @Arendelle No worries! In my opinion this question wasn't rightfully marked as a duplicate, but it's not up to me to decided that. Feel free to ask a question if you can't find any answer that solves your problem. That's what StackOverflow is for, right? :) – schroffl Jun 13 '18 at 13:12