Answer the question
In order to leave comments, you need to log in
Selecting only one image from several?
Hello, I'm asking for help, I'm new to jQuery, I can't figure it out, so I immediately apologize if the question turns out to be stupid, and the solution is simple.
The bottom line is, there is an online store, it has a product card with a description, photo, and several options (choose the color of the product).
<ul class="options-ul">
<li>
<input id="option-value-265" class="optionlbl" type="radio" value="265" name="option[346]">
<label for="option-value-265">
<img alt="Синий" src="blue-50x50.jpg">
</label>
<label for="option-value-265">Синий</label>
</li>
</ul>
<script type="text/javascript"><!--
$('input[name="option[346]"]:radio').on('change', function() {
$(this).closest('li').toggleClass('checked');
});
//--></script>
Answer the question
In order to leave comments, you need to log in
jsfiddle.net/RubaXa/E9juB/ - demo
$('ul.options-ul').on('change', function (evt){
// Убираем выделение
$(this).find('.checked').removeClass('checked', true).prop('checked', false);
// Добавляем выделение
$(evt.target).closest('li').addClass('checked');
});
For example, you can store the selected item in a variable
var currentElement = null;
$('input[name="option[346]"]:radio').on('change', function() {
if (currentElement) currentElement.removeClass('checked');
currentElement = $(this).closest('li').addClass('checked');
});
Then elegant is better.
$( function(){
var selChk = 'li input'; //добавить нужное
$('ul.class_of_ul').on('change', selChk, function(ev){
$(ev.delegateTarget).find(selChk).not($(this).addClass('checked') )
.removeClass('checked').removeAttr('checked');
}); });
It can be like this:
$('ul.options-ul input').on('change', function (){
$(this).closest('li').addClass('checked').siblings().removeClass('checked');
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question