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.

http://jsfiddle.net/du64um5y/

.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

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -