Pain Posted May 29, 2012 Share Posted May 29, 2012 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:)! Quote Link to comment https://forums.phpfreaks.com/topic/263356-jquery-add-id-instead-of-addclass/ Share on other sites More sharing options...
smoseley Posted May 29, 2012 Share Posted May 29, 2012 You can't "add" an id. An id is an "identity" - it must by definition be unique, and one per element. Quote Link to comment https://forums.phpfreaks.com/topic/263356-jquery-add-id-instead-of-addclass/#findComment-1349678 Share on other sites More sharing options...
Pain Posted May 30, 2012 Author Share Posted May 30, 2012 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'); }); Quote Link to comment https://forums.phpfreaks.com/topic/263356-jquery-add-id-instead-of-addclass/#findComment-1349682 Share on other sites More sharing options...
smoseley Posted May 30, 2012 Share Posted May 30, 2012 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 */ } Quote Link to comment https://forums.phpfreaks.com/topic/263356-jquery-add-id-instead-of-addclass/#findComment-1349690 Share on other sites More sharing options...
smoseley Posted May 30, 2012 Share Posted May 30, 2012 Note: instead of mouseover/mouseout, you can also just use 'hover": $(".featured").hover(function(){ $(this).addClass('hover'); }, { $(this).removeClass('hover'); }); Quote Link to comment https://forums.phpfreaks.com/topic/263356-jquery-add-id-instead-of-addclass/#findComment-1349691 Share on other sites More sharing options...
goodacre.liam Posted June 9, 2012 Share Posted June 9, 2012 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'); }); Quote Link to comment https://forums.phpfreaks.com/topic/263356-jquery-add-id-instead-of-addclass/#findComment-1352497 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.