TriSTaR TriSTaR - 1 month ago 9
Javascript Question

Jquery "hasClass" not working

Good morning all,
I used in my site plaxmove.js and i need to set to distinguish for my pages... I wanna use my plaxmove in my fashion blog only left and right without top and bottom when other pages use all.. My code:

if ($('html').hasClass('yu_fashion_layer_4')) {
$('.yu_fashion_header').on('mousemove', function(e) {

x = eqH(e)

$(layer).css({
left: x
})

})
} else {
$('html').on('mousemove', function(e) {

x = eqH(e)
y = eqW(e)

$(layer).css({
left: x,
top: y
})



})
}


It's not working :(

Answer

I assume you are trying to see if a class is there is your html and not the html tag itself. Use .find() for it and check its length.

if ($('html').find('.yu_fashion_layer_4').length > 0) {
  $('.yu_fashion_header').on('mousemove', function(e) {

    x = eqH(e)

    $(layer).css({
      left: x
    })

  })
} else {
  $('html').on('mousemove', function(e) {

    x = eqH(e)
    y = eqW(e)

    $(layer).css({
      left: x,
      top: y
    })



  })
}  

It returns false because your HTML doesn't contain any class, rather some tag in your html will contain that class.

You can also have a selector for the class and then check its length

if ($('.yu_fashion_layer_4').length > 0)
Comments