This question has already been answered:
- How to choose an item? 3 replies
Now when clicking on a picture with a blue border, boarders are painted in red for all blocks. How to make it so that when you click on a specific block, the stroke is colored only in it and the text goes back to not selected?
$('.div').click(function() { if ( !($('.div').hasClass('selected')) ) { $('.div').addClass('selected'); $('.p').text('selected text'); } else { $('.div').removeClass('selected'); } }); .div { width: 100px; height: 100px; border: 1px solid blue; cursor: pointer; } .selected { border-color: red; } <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="div" style="background: url(http://lorempixel.com/output/people-qc-640-480-1.jpg)"></div><p class="p">not selected</p> <div class="div" style="background: url(http://lorempixel.com/output/people-qc-640-480-1.jpg)"></div><p class="p">not selected</p> <div class="div" style="background: url(http://lorempixel.com/output/people-qc-640-480-1.jpg)"></div><p class="p">not selected</p>