What selector can I use to exclude IDs with a suffix?












0














I have a bunch of divs on a page (bg_1, bg_5, bg_112 etc.) that I'd like to assign the style 'display:inline-block'.



However, I have other divs called bg_1_log, bg_5_log, bg_112_log etc..) that I do not want to assign this style to.



I have tried:



 $('[id^=bg_]').attr('style','display:inline-block');


This works, but of course that assigns the style to all divs that start with 'bg_', including bg_112_log...



How can I "assign a style to all divs that are just bg_+number?



I'm using jQuery so either that or JS would be fine.










share|improve this question




















  • 1




    can you assign a class to them while generating the html?
    – Daniel A. White
    Nov 13 at 18:35
















0














I have a bunch of divs on a page (bg_1, bg_5, bg_112 etc.) that I'd like to assign the style 'display:inline-block'.



However, I have other divs called bg_1_log, bg_5_log, bg_112_log etc..) that I do not want to assign this style to.



I have tried:



 $('[id^=bg_]').attr('style','display:inline-block');


This works, but of course that assigns the style to all divs that start with 'bg_', including bg_112_log...



How can I "assign a style to all divs that are just bg_+number?



I'm using jQuery so either that or JS would be fine.










share|improve this question




















  • 1




    can you assign a class to them while generating the html?
    – Daniel A. White
    Nov 13 at 18:35














0












0








0







I have a bunch of divs on a page (bg_1, bg_5, bg_112 etc.) that I'd like to assign the style 'display:inline-block'.



However, I have other divs called bg_1_log, bg_5_log, bg_112_log etc..) that I do not want to assign this style to.



I have tried:



 $('[id^=bg_]').attr('style','display:inline-block');


This works, but of course that assigns the style to all divs that start with 'bg_', including bg_112_log...



How can I "assign a style to all divs that are just bg_+number?



I'm using jQuery so either that or JS would be fine.










share|improve this question















I have a bunch of divs on a page (bg_1, bg_5, bg_112 etc.) that I'd like to assign the style 'display:inline-block'.



However, I have other divs called bg_1_log, bg_5_log, bg_112_log etc..) that I do not want to assign this style to.



I have tried:



 $('[id^=bg_]').attr('style','display:inline-block');


This works, but of course that assigns the style to all divs that start with 'bg_', including bg_112_log...



How can I "assign a style to all divs that are just bg_+number?



I'm using jQuery so either that or JS would be fine.







javascript jquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 at 18:41









isherwood

36.6k1081111




36.6k1081111










asked Nov 13 at 18:33









dijon

149617




149617








  • 1




    can you assign a class to them while generating the html?
    – Daniel A. White
    Nov 13 at 18:35














  • 1




    can you assign a class to them while generating the html?
    – Daniel A. White
    Nov 13 at 18:35








1




1




can you assign a class to them while generating the html?
– Daniel A. White
Nov 13 at 18:35




can you assign a class to them while generating the html?
– Daniel A. White
Nov 13 at 18:35












3 Answers
3






active

oldest

votes


















2














You could use the selector using not and attribute contains selector like:



$('[id^=bg_]').not('[id*=_log]')


Or, you can also use regex as @zfrisch suggested but here's an improved version (Selector not only limitted to div):



$('*').filter(function() { return this.id.match(/bg_d$/) })


Or, even like:



$('*').attr('id').match(/bg_d$/)





share|improve this answer























  • Perfect, I didn't know I could use the .not in conjunction with the contains selector.
    – dijon
    Nov 13 at 21:23



















0














You can use JQuery's .filter method






$('div')
.filter(function() {
return this.id.match(/bg_d$/);
})
.css("backgroundColor", "green");

div:after {
content: "test";
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="bg_1"></div>
<div id="bg_2"></div>
<div id="bg_3"></div>
<div id="bg_4"></div>
<div id="bg_5"></div>
<div id="bg_6"></div>
<div id="bg_1_log"></div>
<div id="bg_2_log"></div>
<div id="bg_3_log"></div>
<div id="bg_4_log"></div>
<div id="bg_5_log"></div>
<div id="bg_6_log"></div>





Or as another answer pointed out .not






share|improve this answer





























    0














    If you don't want to select the elements whose id ends with _log then you can use following code:



    $('[id^=bg_]').not('[id$=_log]').attr('style','display:inline-block');





    share|improve this answer





















      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287445%2fwhat-selector-can-i-use-to-exclude-ids-with-a-suffix%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      You could use the selector using not and attribute contains selector like:



      $('[id^=bg_]').not('[id*=_log]')


      Or, you can also use regex as @zfrisch suggested but here's an improved version (Selector not only limitted to div):



      $('*').filter(function() { return this.id.match(/bg_d$/) })


      Or, even like:



      $('*').attr('id').match(/bg_d$/)





      share|improve this answer























      • Perfect, I didn't know I could use the .not in conjunction with the contains selector.
        – dijon
        Nov 13 at 21:23
















      2














      You could use the selector using not and attribute contains selector like:



      $('[id^=bg_]').not('[id*=_log]')


      Or, you can also use regex as @zfrisch suggested but here's an improved version (Selector not only limitted to div):



      $('*').filter(function() { return this.id.match(/bg_d$/) })


      Or, even like:



      $('*').attr('id').match(/bg_d$/)





      share|improve this answer























      • Perfect, I didn't know I could use the .not in conjunction with the contains selector.
        – dijon
        Nov 13 at 21:23














      2












      2








      2






      You could use the selector using not and attribute contains selector like:



      $('[id^=bg_]').not('[id*=_log]')


      Or, you can also use regex as @zfrisch suggested but here's an improved version (Selector not only limitted to div):



      $('*').filter(function() { return this.id.match(/bg_d$/) })


      Or, even like:



      $('*').attr('id').match(/bg_d$/)





      share|improve this answer














      You could use the selector using not and attribute contains selector like:



      $('[id^=bg_]').not('[id*=_log]')


      Or, you can also use regex as @zfrisch suggested but here's an improved version (Selector not only limitted to div):



      $('*').filter(function() { return this.id.match(/bg_d$/) })


      Or, even like:



      $('*').attr('id').match(/bg_d$/)






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 13 at 18:55

























      answered Nov 13 at 18:36









      Bhojendra Rauniyar

      50.7k2079124




      50.7k2079124












      • Perfect, I didn't know I could use the .not in conjunction with the contains selector.
        – dijon
        Nov 13 at 21:23


















      • Perfect, I didn't know I could use the .not in conjunction with the contains selector.
        – dijon
        Nov 13 at 21:23
















      Perfect, I didn't know I could use the .not in conjunction with the contains selector.
      – dijon
      Nov 13 at 21:23




      Perfect, I didn't know I could use the .not in conjunction with the contains selector.
      – dijon
      Nov 13 at 21:23













      0














      You can use JQuery's .filter method






      $('div')
      .filter(function() {
      return this.id.match(/bg_d$/);
      })
      .css("backgroundColor", "green");

      div:after {
      content: "test";
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <div id="bg_1"></div>
      <div id="bg_2"></div>
      <div id="bg_3"></div>
      <div id="bg_4"></div>
      <div id="bg_5"></div>
      <div id="bg_6"></div>
      <div id="bg_1_log"></div>
      <div id="bg_2_log"></div>
      <div id="bg_3_log"></div>
      <div id="bg_4_log"></div>
      <div id="bg_5_log"></div>
      <div id="bg_6_log"></div>





      Or as another answer pointed out .not






      share|improve this answer


























        0














        You can use JQuery's .filter method






        $('div')
        .filter(function() {
        return this.id.match(/bg_d$/);
        })
        .css("backgroundColor", "green");

        div:after {
        content: "test";
        }

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        <div id="bg_1"></div>
        <div id="bg_2"></div>
        <div id="bg_3"></div>
        <div id="bg_4"></div>
        <div id="bg_5"></div>
        <div id="bg_6"></div>
        <div id="bg_1_log"></div>
        <div id="bg_2_log"></div>
        <div id="bg_3_log"></div>
        <div id="bg_4_log"></div>
        <div id="bg_5_log"></div>
        <div id="bg_6_log"></div>





        Or as another answer pointed out .not






        share|improve this answer
























          0












          0








          0






          You can use JQuery's .filter method






          $('div')
          .filter(function() {
          return this.id.match(/bg_d$/);
          })
          .css("backgroundColor", "green");

          div:after {
          content: "test";
          }

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <div id="bg_1"></div>
          <div id="bg_2"></div>
          <div id="bg_3"></div>
          <div id="bg_4"></div>
          <div id="bg_5"></div>
          <div id="bg_6"></div>
          <div id="bg_1_log"></div>
          <div id="bg_2_log"></div>
          <div id="bg_3_log"></div>
          <div id="bg_4_log"></div>
          <div id="bg_5_log"></div>
          <div id="bg_6_log"></div>





          Or as another answer pointed out .not






          share|improve this answer












          You can use JQuery's .filter method






          $('div')
          .filter(function() {
          return this.id.match(/bg_d$/);
          })
          .css("backgroundColor", "green");

          div:after {
          content: "test";
          }

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <div id="bg_1"></div>
          <div id="bg_2"></div>
          <div id="bg_3"></div>
          <div id="bg_4"></div>
          <div id="bg_5"></div>
          <div id="bg_6"></div>
          <div id="bg_1_log"></div>
          <div id="bg_2_log"></div>
          <div id="bg_3_log"></div>
          <div id="bg_4_log"></div>
          <div id="bg_5_log"></div>
          <div id="bg_6_log"></div>





          Or as another answer pointed out .not






          $('div')
          .filter(function() {
          return this.id.match(/bg_d$/);
          })
          .css("backgroundColor", "green");

          div:after {
          content: "test";
          }

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <div id="bg_1"></div>
          <div id="bg_2"></div>
          <div id="bg_3"></div>
          <div id="bg_4"></div>
          <div id="bg_5"></div>
          <div id="bg_6"></div>
          <div id="bg_1_log"></div>
          <div id="bg_2_log"></div>
          <div id="bg_3_log"></div>
          <div id="bg_4_log"></div>
          <div id="bg_5_log"></div>
          <div id="bg_6_log"></div>





          $('div')
          .filter(function() {
          return this.id.match(/bg_d$/);
          })
          .css("backgroundColor", "green");

          div:after {
          content: "test";
          }

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
          <div id="bg_1"></div>
          <div id="bg_2"></div>
          <div id="bg_3"></div>
          <div id="bg_4"></div>
          <div id="bg_5"></div>
          <div id="bg_6"></div>
          <div id="bg_1_log"></div>
          <div id="bg_2_log"></div>
          <div id="bg_3_log"></div>
          <div id="bg_4_log"></div>
          <div id="bg_5_log"></div>
          <div id="bg_6_log"></div>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 at 18:42









          zfrisch

          4,42311024




          4,42311024























              0














              If you don't want to select the elements whose id ends with _log then you can use following code:



              $('[id^=bg_]').not('[id$=_log]').attr('style','display:inline-block');





              share|improve this answer


























                0














                If you don't want to select the elements whose id ends with _log then you can use following code:



                $('[id^=bg_]').not('[id$=_log]').attr('style','display:inline-block');





                share|improve this answer
























                  0












                  0








                  0






                  If you don't want to select the elements whose id ends with _log then you can use following code:



                  $('[id^=bg_]').not('[id$=_log]').attr('style','display:inline-block');





                  share|improve this answer












                  If you don't want to select the elements whose id ends with _log then you can use following code:



                  $('[id^=bg_]').not('[id$=_log]').attr('style','display:inline-block');






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 at 18:44









                  Gowri Sankar K

                  1




                  1






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.





                      Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                      Please pay close attention to the following guidance:


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287445%2fwhat-selector-can-i-use-to-exclude-ids-with-a-suffix%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Guess what letter conforming each word

                      Port of Spain

                      Run scheduled task as local user group (not BUILTIN)