What selector can I use to exclude IDs with a suffix?
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
add a comment |
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
1
can you assign a class to them while generating the html?
– Daniel A. White
Nov 13 at 18:35
add a comment |
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
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
javascript jquery
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
add a comment |
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
add a comment |
3 Answers
3
active
oldest
votes
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$/)
Perfect, I didn't know I could use the .not in conjunction with the contains selector.
– dijon
Nov 13 at 21:23
add a comment |
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
add a comment |
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');
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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$/)
Perfect, I didn't know I could use the .not in conjunction with the contains selector.
– dijon
Nov 13 at 21:23
add a comment |
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$/)
Perfect, I didn't know I could use the .not in conjunction with the contains selector.
– dijon
Nov 13 at 21:23
add a comment |
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$/)
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$/)
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
add a comment |
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
add a comment |
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
add a comment |
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
add a comment |
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
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>
answered Nov 13 at 18:42
zfrisch
4,42311024
4,42311024
add a comment |
add a comment |
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');
add a comment |
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');
add a comment |
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');
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');
answered Nov 13 at 18:44
Gowri Sankar K
1
1
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
can you assign a class to them while generating the html?
– Daniel A. White
Nov 13 at 18:35