Johan
Johan

Reputation: 35194

checking if element with class exists

var currentClassName = $(this).parent('td').next('td').find('input:text').attr('class');

                if (currentClassName == undefined) {
                    currentClassName = $(this).parent('td').find('input:text').attr('class');
                }

This works, but i suppose it can be written in one row? Im not really satisfied with this solution. Thanks

Upvotes: 0

Views: 1688

Answers (3)

Ryan Atallah
Ryan Atallah

Reputation: 2987

Actually, I think you should be able to check the existence of a class using the attribute jQuery selector:

if ($(this).parent('td').next('td').find('input:text[class]').length != 0) {

  // do something

}

Upvotes: 0

Andreas Eriksson
Andreas Eriksson

Reputation: 9027

var currentClassName = $(this).parent('td').next('td').find('input:text').attr('class');
currentClassName = (currentClassName == undefined ? $(this).parent('td').find('input:text').attr('class') : currentClassName);

Upvotes: 0

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

if ($(#elm).is('.classname')) {
//#elm has the class
} else {
//#elm doesn't have the class
}

Upvotes: 1

Related Questions