How to use if on a specific string
I want to use an if statement to see if something has hidden written in it. The div classname is ".title-button" and I want to use in javascript or jquery if {.title-button = "Hidden"){...}
but it doesn't work.
This is for a side project of mine. I have researched it lots of time but I cant find anything
if(".title-button" = "hidden"){
}
is the only code I have, but it doesn't work.
My expected result is to be able to check the string in the class, I don't get an actual result.
javascript jquery string if-statement
|
show 4 more comments
I want to use an if statement to see if something has hidden written in it. The div classname is ".title-button" and I want to use in javascript or jquery if {.title-button = "Hidden"){...}
but it doesn't work.
This is for a side project of mine. I have researched it lots of time but I cant find anything
if(".title-button" = "hidden"){
}
is the only code I have, but it doesn't work.
My expected result is to be able to check the string in the class, I don't get an actual result.
javascript jquery string if-statement
So there are at least three things wrong with this. But before that, what is your intention of checking "hidden"? Are you trying to see if the element is hidden?
– Taplar
Jan 2 at 18:04
UsejQuery#hasClass
:if($(".title-button").hasClass("hidden")) {
– ibrahim mahrir
Jan 2 at 18:04
I'm trying to see if the element contains the word "hidden"
– flugs
Jan 2 at 18:06
So you're trying to see if<div class="title-button">hidden</div>
exists? That's what "contains the word" would mean
– Taplar
Jan 2 at 18:07
or if<div class="title-button">My Button</div>
is visible (actually hidden) at the moment?
– Steve0
Jan 2 at 18:08
|
show 4 more comments
I want to use an if statement to see if something has hidden written in it. The div classname is ".title-button" and I want to use in javascript or jquery if {.title-button = "Hidden"){...}
but it doesn't work.
This is for a side project of mine. I have researched it lots of time but I cant find anything
if(".title-button" = "hidden"){
}
is the only code I have, but it doesn't work.
My expected result is to be able to check the string in the class, I don't get an actual result.
javascript jquery string if-statement
I want to use an if statement to see if something has hidden written in it. The div classname is ".title-button" and I want to use in javascript or jquery if {.title-button = "Hidden"){...}
but it doesn't work.
This is for a side project of mine. I have researched it lots of time but I cant find anything
if(".title-button" = "hidden"){
}
is the only code I have, but it doesn't work.
My expected result is to be able to check the string in the class, I don't get an actual result.
javascript jquery string if-statement
javascript jquery string if-statement
edited Jan 2 at 18:07


Shahnewaz
392312
392312
asked Jan 2 at 18:02


flugsflugs
214
214
So there are at least three things wrong with this. But before that, what is your intention of checking "hidden"? Are you trying to see if the element is hidden?
– Taplar
Jan 2 at 18:04
UsejQuery#hasClass
:if($(".title-button").hasClass("hidden")) {
– ibrahim mahrir
Jan 2 at 18:04
I'm trying to see if the element contains the word "hidden"
– flugs
Jan 2 at 18:06
So you're trying to see if<div class="title-button">hidden</div>
exists? That's what "contains the word" would mean
– Taplar
Jan 2 at 18:07
or if<div class="title-button">My Button</div>
is visible (actually hidden) at the moment?
– Steve0
Jan 2 at 18:08
|
show 4 more comments
So there are at least three things wrong with this. But before that, what is your intention of checking "hidden"? Are you trying to see if the element is hidden?
– Taplar
Jan 2 at 18:04
UsejQuery#hasClass
:if($(".title-button").hasClass("hidden")) {
– ibrahim mahrir
Jan 2 at 18:04
I'm trying to see if the element contains the word "hidden"
– flugs
Jan 2 at 18:06
So you're trying to see if<div class="title-button">hidden</div>
exists? That's what "contains the word" would mean
– Taplar
Jan 2 at 18:07
or if<div class="title-button">My Button</div>
is visible (actually hidden) at the moment?
– Steve0
Jan 2 at 18:08
So there are at least three things wrong with this. But before that, what is your intention of checking "hidden"? Are you trying to see if the element is hidden?
– Taplar
Jan 2 at 18:04
So there are at least three things wrong with this. But before that, what is your intention of checking "hidden"? Are you trying to see if the element is hidden?
– Taplar
Jan 2 at 18:04
Use
jQuery#hasClass
: if($(".title-button").hasClass("hidden")) {
– ibrahim mahrir
Jan 2 at 18:04
Use
jQuery#hasClass
: if($(".title-button").hasClass("hidden")) {
– ibrahim mahrir
Jan 2 at 18:04
I'm trying to see if the element contains the word "hidden"
– flugs
Jan 2 at 18:06
I'm trying to see if the element contains the word "hidden"
– flugs
Jan 2 at 18:06
So you're trying to see if
<div class="title-button">hidden</div>
exists? That's what "contains the word" would mean– Taplar
Jan 2 at 18:07
So you're trying to see if
<div class="title-button">hidden</div>
exists? That's what "contains the word" would mean– Taplar
Jan 2 at 18:07
or if
<div class="title-button">My Button</div>
is visible (actually hidden) at the moment?– Steve0
Jan 2 at 18:08
or if
<div class="title-button">My Button</div>
is visible (actually hidden) at the moment?– Steve0
Jan 2 at 18:08
|
show 4 more comments
3 Answers
3
active
oldest
votes
If you are trying to check if the element has the text of hidden
, then that would be something like.
if ($('.title-button').text() === 'hidden') {
console.log('yes');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="title-button">hidden</div>
There are various issues with your original attempt:
if(".title-button" = "hidden"){}
1) You are not looking up the element
".title-button"
is just a string. It's not an element lookup. To use jQuery to actually look the element up, you have to use $(".title-button")
.
2) =
is assignment
Assignment is meant to be used with variables (ex. var x = 3
), not comparison. Comparisons are performed with ==
or ===
, the second one not performing type coersion.
add a comment |
Your current code says "set the string .title-button
equal to the string hidden
and, if that value is truthy (i.e. true, a nonzero number, a nonempty string, etc.), then do something". That makes no sense. So let's take this step-by-step.
"My expected result is to be able to check the string in the class."
What is "the string in the class"? A class name is a string, which simply indicates a group that an element belongs to. If you're trying to check whether an element which has the class title-button
also has the class hidden
, then you'd want to first get the element -- not just its class name -- and check all its classes for the class hidden
. With jQuery, you can get an element from one of its classes by using $('.class-name')
, so in this case, $('.title-button')
. Then you can check if it has a certain class using the .hasClass('new-class-name
)method. So
if ($('.title-button').hasClass('hidden')) {` is the condition you want to check.
If instead you're trying to check if the contents of the element contain the string "hidden", then you need to first get the contents and then check if it contains what you want. You can do this a few ways, but here's just one: if ($('.title-button').html().indexOf('hidden') >= 0) {
(For reference, the other methods instead of using indexOf would be .includes
and .match
; you could also use .test
, though that has different syntax since the Regular Expression comes first, so I didn't show it here.)
add a comment |
If you are trying to find the text "hidden" in the div elements content,for example
<div class="title-button">I am hidden</div>
If(document.querySelector(".title-button").innerHTML.indexOf("hidden") != -1){
//Your statement goes here
}
If you are trying to find that the div element has class "hidden" in its class list,then there are two scenarios,
1)If you have multiple classes on the div element.For example,
<div class="title-button class2 class3"></div>
If(document.querySelector(".title-button").className.indexOf("hidden") != -1){
//Your statement goes here
}
2)If you have only one class as follows,
<div class="title-button"></div>
If(document.querySelector(".title-button").className == "hidden"){
//Your statement goes here
}
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%2f54011061%2fhow-to-use-if-on-a-specific-string%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
If you are trying to check if the element has the text of hidden
, then that would be something like.
if ($('.title-button').text() === 'hidden') {
console.log('yes');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="title-button">hidden</div>
There are various issues with your original attempt:
if(".title-button" = "hidden"){}
1) You are not looking up the element
".title-button"
is just a string. It's not an element lookup. To use jQuery to actually look the element up, you have to use $(".title-button")
.
2) =
is assignment
Assignment is meant to be used with variables (ex. var x = 3
), not comparison. Comparisons are performed with ==
or ===
, the second one not performing type coersion.
add a comment |
If you are trying to check if the element has the text of hidden
, then that would be something like.
if ($('.title-button').text() === 'hidden') {
console.log('yes');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="title-button">hidden</div>
There are various issues with your original attempt:
if(".title-button" = "hidden"){}
1) You are not looking up the element
".title-button"
is just a string. It's not an element lookup. To use jQuery to actually look the element up, you have to use $(".title-button")
.
2) =
is assignment
Assignment is meant to be used with variables (ex. var x = 3
), not comparison. Comparisons are performed with ==
or ===
, the second one not performing type coersion.
add a comment |
If you are trying to check if the element has the text of hidden
, then that would be something like.
if ($('.title-button').text() === 'hidden') {
console.log('yes');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="title-button">hidden</div>
There are various issues with your original attempt:
if(".title-button" = "hidden"){}
1) You are not looking up the element
".title-button"
is just a string. It's not an element lookup. To use jQuery to actually look the element up, you have to use $(".title-button")
.
2) =
is assignment
Assignment is meant to be used with variables (ex. var x = 3
), not comparison. Comparisons are performed with ==
or ===
, the second one not performing type coersion.
If you are trying to check if the element has the text of hidden
, then that would be something like.
if ($('.title-button').text() === 'hidden') {
console.log('yes');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="title-button">hidden</div>
There are various issues with your original attempt:
if(".title-button" = "hidden"){}
1) You are not looking up the element
".title-button"
is just a string. It's not an element lookup. To use jQuery to actually look the element up, you have to use $(".title-button")
.
2) =
is assignment
Assignment is meant to be used with variables (ex. var x = 3
), not comparison. Comparisons are performed with ==
or ===
, the second one not performing type coersion.
if ($('.title-button').text() === 'hidden') {
console.log('yes');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="title-button">hidden</div>
if ($('.title-button').text() === 'hidden') {
console.log('yes');
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="title-button">hidden</div>
answered Jan 2 at 18:13
TaplarTaplar
17.8k21529
17.8k21529
add a comment |
add a comment |
Your current code says "set the string .title-button
equal to the string hidden
and, if that value is truthy (i.e. true, a nonzero number, a nonempty string, etc.), then do something". That makes no sense. So let's take this step-by-step.
"My expected result is to be able to check the string in the class."
What is "the string in the class"? A class name is a string, which simply indicates a group that an element belongs to. If you're trying to check whether an element which has the class title-button
also has the class hidden
, then you'd want to first get the element -- not just its class name -- and check all its classes for the class hidden
. With jQuery, you can get an element from one of its classes by using $('.class-name')
, so in this case, $('.title-button')
. Then you can check if it has a certain class using the .hasClass('new-class-name
)method. So
if ($('.title-button').hasClass('hidden')) {` is the condition you want to check.
If instead you're trying to check if the contents of the element contain the string "hidden", then you need to first get the contents and then check if it contains what you want. You can do this a few ways, but here's just one: if ($('.title-button').html().indexOf('hidden') >= 0) {
(For reference, the other methods instead of using indexOf would be .includes
and .match
; you could also use .test
, though that has different syntax since the Regular Expression comes first, so I didn't show it here.)
add a comment |
Your current code says "set the string .title-button
equal to the string hidden
and, if that value is truthy (i.e. true, a nonzero number, a nonempty string, etc.), then do something". That makes no sense. So let's take this step-by-step.
"My expected result is to be able to check the string in the class."
What is "the string in the class"? A class name is a string, which simply indicates a group that an element belongs to. If you're trying to check whether an element which has the class title-button
also has the class hidden
, then you'd want to first get the element -- not just its class name -- and check all its classes for the class hidden
. With jQuery, you can get an element from one of its classes by using $('.class-name')
, so in this case, $('.title-button')
. Then you can check if it has a certain class using the .hasClass('new-class-name
)method. So
if ($('.title-button').hasClass('hidden')) {` is the condition you want to check.
If instead you're trying to check if the contents of the element contain the string "hidden", then you need to first get the contents and then check if it contains what you want. You can do this a few ways, but here's just one: if ($('.title-button').html().indexOf('hidden') >= 0) {
(For reference, the other methods instead of using indexOf would be .includes
and .match
; you could also use .test
, though that has different syntax since the Regular Expression comes first, so I didn't show it here.)
add a comment |
Your current code says "set the string .title-button
equal to the string hidden
and, if that value is truthy (i.e. true, a nonzero number, a nonempty string, etc.), then do something". That makes no sense. So let's take this step-by-step.
"My expected result is to be able to check the string in the class."
What is "the string in the class"? A class name is a string, which simply indicates a group that an element belongs to. If you're trying to check whether an element which has the class title-button
also has the class hidden
, then you'd want to first get the element -- not just its class name -- and check all its classes for the class hidden
. With jQuery, you can get an element from one of its classes by using $('.class-name')
, so in this case, $('.title-button')
. Then you can check if it has a certain class using the .hasClass('new-class-name
)method. So
if ($('.title-button').hasClass('hidden')) {` is the condition you want to check.
If instead you're trying to check if the contents of the element contain the string "hidden", then you need to first get the contents and then check if it contains what you want. You can do this a few ways, but here's just one: if ($('.title-button').html().indexOf('hidden') >= 0) {
(For reference, the other methods instead of using indexOf would be .includes
and .match
; you could also use .test
, though that has different syntax since the Regular Expression comes first, so I didn't show it here.)
Your current code says "set the string .title-button
equal to the string hidden
and, if that value is truthy (i.e. true, a nonzero number, a nonempty string, etc.), then do something". That makes no sense. So let's take this step-by-step.
"My expected result is to be able to check the string in the class."
What is "the string in the class"? A class name is a string, which simply indicates a group that an element belongs to. If you're trying to check whether an element which has the class title-button
also has the class hidden
, then you'd want to first get the element -- not just its class name -- and check all its classes for the class hidden
. With jQuery, you can get an element from one of its classes by using $('.class-name')
, so in this case, $('.title-button')
. Then you can check if it has a certain class using the .hasClass('new-class-name
)method. So
if ($('.title-button').hasClass('hidden')) {` is the condition you want to check.
If instead you're trying to check if the contents of the element contain the string "hidden", then you need to first get the contents and then check if it contains what you want. You can do this a few ways, but here's just one: if ($('.title-button').html().indexOf('hidden') >= 0) {
(For reference, the other methods instead of using indexOf would be .includes
and .match
; you could also use .test
, though that has different syntax since the Regular Expression comes first, so I didn't show it here.)
answered Jan 2 at 18:13
IceMetalPunkIceMetalPunk
989716
989716
add a comment |
add a comment |
If you are trying to find the text "hidden" in the div elements content,for example
<div class="title-button">I am hidden</div>
If(document.querySelector(".title-button").innerHTML.indexOf("hidden") != -1){
//Your statement goes here
}
If you are trying to find that the div element has class "hidden" in its class list,then there are two scenarios,
1)If you have multiple classes on the div element.For example,
<div class="title-button class2 class3"></div>
If(document.querySelector(".title-button").className.indexOf("hidden") != -1){
//Your statement goes here
}
2)If you have only one class as follows,
<div class="title-button"></div>
If(document.querySelector(".title-button").className == "hidden"){
//Your statement goes here
}
add a comment |
If you are trying to find the text "hidden" in the div elements content,for example
<div class="title-button">I am hidden</div>
If(document.querySelector(".title-button").innerHTML.indexOf("hidden") != -1){
//Your statement goes here
}
If you are trying to find that the div element has class "hidden" in its class list,then there are two scenarios,
1)If you have multiple classes on the div element.For example,
<div class="title-button class2 class3"></div>
If(document.querySelector(".title-button").className.indexOf("hidden") != -1){
//Your statement goes here
}
2)If you have only one class as follows,
<div class="title-button"></div>
If(document.querySelector(".title-button").className == "hidden"){
//Your statement goes here
}
add a comment |
If you are trying to find the text "hidden" in the div elements content,for example
<div class="title-button">I am hidden</div>
If(document.querySelector(".title-button").innerHTML.indexOf("hidden") != -1){
//Your statement goes here
}
If you are trying to find that the div element has class "hidden" in its class list,then there are two scenarios,
1)If you have multiple classes on the div element.For example,
<div class="title-button class2 class3"></div>
If(document.querySelector(".title-button").className.indexOf("hidden") != -1){
//Your statement goes here
}
2)If you have only one class as follows,
<div class="title-button"></div>
If(document.querySelector(".title-button").className == "hidden"){
//Your statement goes here
}
If you are trying to find the text "hidden" in the div elements content,for example
<div class="title-button">I am hidden</div>
If(document.querySelector(".title-button").innerHTML.indexOf("hidden") != -1){
//Your statement goes here
}
If you are trying to find that the div element has class "hidden" in its class list,then there are two scenarios,
1)If you have multiple classes on the div element.For example,
<div class="title-button class2 class3"></div>
If(document.querySelector(".title-button").className.indexOf("hidden") != -1){
//Your statement goes here
}
2)If you have only one class as follows,
<div class="title-button"></div>
If(document.querySelector(".title-button").className == "hidden"){
//Your statement goes here
}
edited Jan 2 at 18:33
answered Jan 2 at 18:25
Aravind Kumar JAravind Kumar J
11
11
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.
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%2f54011061%2fhow-to-use-if-on-a-specific-string%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
So there are at least three things wrong with this. But before that, what is your intention of checking "hidden"? Are you trying to see if the element is hidden?
– Taplar
Jan 2 at 18:04
Use
jQuery#hasClass
:if($(".title-button").hasClass("hidden")) {
– ibrahim mahrir
Jan 2 at 18:04
I'm trying to see if the element contains the word "hidden"
– flugs
Jan 2 at 18:06
So you're trying to see if
<div class="title-button">hidden</div>
exists? That's what "contains the word" would mean– Taplar
Jan 2 at 18:07
or if
<div class="title-button">My Button</div>
is visible (actually hidden) at the moment?– Steve0
Jan 2 at 18:08