Jump to content

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');      
});

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.