jquery siblings attributes to array
Anyone knows how can I get value of the attribute 'rel' from all siblings (labels of the radio button group) into array?
I'开发者_如何学JAVAve tried something like this, but it obviously doesn't work:
<label rel="option_local" class="type_radio" for="type_3">
<input type="radio" checked="checked" value="3" id="type_3" name="type" />
Local</label>
<label rel="option_remote" class="type_radio" for="type_2">
<input type="radio" value="2" id="type_2" name="type" />
Remote</label>
<label rel="option_html" class="type_radio" for="type_1">
<input type="radio" value="1" id="type_1" name="type" />
HTML</label>
$('.type_radio').click(function() {
var r = $(this).attr('rel');
var arr = $(this).siblings().attr('rel');
$.each(arr, function(k, v) {
$('.' + v).addClass('dn');
});
$('.' + r).removeClass('dn');
return false;
});
I'm trying to loop through elements with classes, which refer to the 'rel' attribute of the specific trigger and remove specific class from them. Then apply this class to the ones which refer to the selected one.
All labels have class 'type_radio'.
Ok - I've managed to achieve it - here's how:
$('.type_radio').click(function() {
var r = $(this).attr('rel');
var arr = $(this).siblings();
var tr;
$.each(arr, function() {
tr = $(this).attr('rel');
$('.' + tr).addClass('dn');
});
$('.' + r).removeClass('dn');
});
精彩评论