Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript if else statement issue

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #31018
    noahgelman
    Participant

    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?

    #69686
    Bob
    Member

    I 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?

    #69671
    mdgrech
    Member

    var element = get.YourElement

    if (element.hasClass(‘foo’)) { do.something }

    #69629
    noahgelman
    Participant

    @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.

    #69606
    TT_Mark
    Member

    @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!

    #69554
    noahgelman
    Participant

    Yup, 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

    #69557
    Bob
    Member

    @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.

    #69535
    Bob
    Member

    I 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..

    #69537
    TT_Mark
    Member

    Noah 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

    #69457
    Chris Coyier
    Keymaster

    var 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.

    #69464
    noahgelman
    Participant

    But 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.

    #69466
    Rob MacKay
    Participant

    That’s why jQuery is pure awesome. :)

    #69468
    noahgelman
    Participant

    So there’s no easy way to do that in javascript is there? :(

    #69469
    Chris Coyier
    Keymaster

    Well 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

    #69471
    noahgelman
    Participant

    This 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?

Viewing 15 posts - 1 through 15 (of 15 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.