Jump to content

jQuery add id instead of addClass?


Pain

Recommended Posts

Hello there.

 

Was wondering if there is such a thing as addClass or something else which would be similar to .addClass

 

For instance i have this code:

 

$("#featured").mouseover(function(){

    $(this).addClass('borders2');

    }).mouseout(function(){

    $(this).addClass('borders');

    });

 

Can you help me out with this? Thank you:)!

Link to comment
https://forums.phpfreaks.com/topic/263356-jquery-add-id-instead-of-addclass/
Share on other sites

Ok, got it.

 

However my code doesn't work.

 

If i have a div with a class "featured" and i want to apply a "borders2" class when hovered then should this code work?

 

 

$(".featured").mouseover(function(){

      $(this).addClass('borders2');

    }).mouseout(function(){

      $(this).addClass('borders');     

    });

Try just adding and removing the class... 

 

$(".featured").mouseover(function(){

      $(this).addClass('hover');

    }).mouseout(function(){

      $(this).removeClass('hover');     

    });

 

then in your CSS, use these selectors to style your element

 

.featured {

    /* Normal style */

}

 

.featured.hover {

    /* Hover style */

}

  • 2 weeks later...

To set the id of an element, you can use the 'attr' method:

//  setter
$('...selector...').attr('id', 'thenewid');
//  getter
var id = $('...selector...').attr('id');

 

I would say that it would be better to use add/remove class as smoseley suggested.  Although I don't fully know your situation, ids may be applicable.

 

@smoseley:

You missed out the function keyword from the second procedure.

 

$(".featured").hover(function () {
    $(this).addClass('hover');
}, function () {
    $(this).removeClass('hover');      
});

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.