Home › Forums › JavaScript › if else statement issue
- This topic is empty.
-
AuthorPosts
-
December 16, 2010 at 2:22 am #31018noahgelmanParticipant
I want to write an if else statement in just javascript, no jquery. I want the condition to be whether a specific element has a certain class or not, how can I do this?
December 16, 2010 at 5:14 am #69686BobMemberI don’t think you can do it with plain Javascript and you would nee jquery for this. Javascript hasn’t got a function to get elements by their class as far as I know.
It does however has getElementById, so you can select elements based on a specific ID.
Why don’t you want to use jquery?
December 16, 2010 at 10:26 am #69671mdgrechMembervar element = get.YourElement
if (element.hasClass(‘foo’)) { do.something }
December 16, 2010 at 12:40 pm #69629noahgelmanParticipant@bob, thanks. It’s weird that javascript doesn’t have a function to get elements by class, or at least detect whether an element has a certain class or not. I didn’t want to use jquery because the rest of the related scripts are pure javascript and I didn’t want any issues with it having to wait to load jquery before the scripts could run properly. I dont think I’ll have any of those troubles though, it seems to be working just fine with mdgrech’s suggestion.
@mdgrech, thank you too. that was the same solution I came up with as well. Unfortunately it uses jQuery’s .hasClass() but its fine, win some lose some.December 16, 2010 at 3:42 pm #69606TT_MarkMember@Bob, sorry but if JavaScript cannot do it then how the hell do you think JQuery can?
@noahgelman I believe you can do the following:if ( document.getElementById("test").getAttribute("class") == 'class')
I haven’t tested this though!
December 17, 2010 at 3:35 pm #69554noahgelmanParticipantYup, that worked perfectly. I’m used to using jQuery for all my problems. My javascript isn’t up to snuff. jQuery isn’t option on the current project so I’m really learning a lot of javascript
December 17, 2010 at 6:18 pm #69557BobMember@TT_Mark, does Javascript has a function to get elements by their class then? And I don’t mean the way you posted it above, but something like getElementByClass. I don’t think it does, hence I posted that.
December 18, 2010 at 5:04 am #69535BobMemberI know, but what I meant is there is no function in Javascript as far as I know that can get elements by their class directly, only indirectly via the example TT_Mark gave. Or can Javascript do it directly? Thats all I said really..
December 18, 2010 at 5:53 am #69537TT_MarkMemberNoah never asked for a function that checked if the element had a certain class, he simply asked for the code to do it without using JQuery and your response was that it couldn’t be done without JQuery.
I was simply pointing out that JQuery is nothing more than JavaScript simplified. I assume the JQuery .hasClass() function is built on top of the code I posted above
December 18, 2010 at 11:28 am #69457Chris CoyierKeymastervar test = document.getElementById('test');
var class = test.className;
if (class == 'thing') {
// yeah buddy
} else {
// nope darling
}Remember that classes can be space-separated though, so you’d likely need to split them into an array based on spaces and then loop through that array testing each one.
December 18, 2010 at 4:02 pm #69464noahgelmanParticipantBut in jQuery, you can select all classes on the page like this: $(‘.class’).dosomething(). I think Bob was talking about a javascript equivalent to that.
December 18, 2010 at 4:35 pm #69466Rob MacKayParticipantThat’s why jQuery is pure awesome. :)
December 18, 2010 at 5:27 pm #69468noahgelmanParticipantSo there’s no easy way to do that in javascript is there? :(
December 18, 2010 at 7:46 pm #69469Chris CoyierKeymasterWell there is: https://developer.mozilla.org/en/DOM/document.getElementsByClassName
It’s just not supported in all browsers. So… there are little scripts that use to simulate it. http://snook.ca/archives/javascript/your_favourite_1
December 18, 2010 at 7:57 pm #69471noahgelmanParticipantThis turned into more of an interesting topic than I expected.
.getElementsByClassName(names) works in firefox obviously. What versions of Chrome, Safari, Opera, and IE support it, if they do?
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.