html - JQuery addclass/removeclass by div ID -
i want addclass()/removeclass() divs different class name , try find them id.
how can make work?
js:
$(document).ready(function() { $("#button_hof").click(function() { $("#button_hof").removeclass("selected"); $(this).addclass("selected"); return false; }); }); html:
<div id="button_hof" class="button_hof selected"></div> <div id="button_hof" class="button_hof"></div> <div id="button_hof" class="button_hof2"></div> <div id="button_hof" class="button_hof2"></div>
you not need have id's on each of items watching. other commenter stated, id on element must unique.
.selected { color: hotpink; } <div class="button_hof selected">1</div> <div class="button_hof">2</div> <div class="button_hof">3</div> <div class="button_hof">4</div> var allbuttons = jquery('.button_hof'); jquery('.button_hof').click(function(e){ var $active = jquery(this); allbuttons.removeclass('selected'); $active.addclass('selected'); });
Comments
Post a Comment