Trying to get the parent class of an element using jQuery

So, I purchased the element that I want, but I want to check the value of the parent element class to make sure that it is a specific value before doing my logic, and I cannot figure it out.

$(document).ready(function() {
    $('#childElement input').click(function() {
        var option = $(this).parent('span').class;
        alert(option.class);
        if (option == 'left') {
            //some action
        }
});

HTML:

<span class="left">
<input type="checkbox" tabindex="401"><label>some verbage</label></span>

I can run a check on the checkbox, but I want to get the class value for the span element (which, in my opinion, is the parent of the input element. Then do an if comparison to check if the class = = "left".

Any thoughts what I'm doing wrong?

+3
source share
1 answer

You can check with .hasClass(), for example:

if($(this).parent('span').hasClass('left')) {

.class .length, :

if($(this).parent('span.left').length) {

class, , .attr('class') .class:)

+4

Source: https://habr.com/ru/post/1760578/


All Articles