Reputation: 3
I have a drop down of 7 options. I need to get the value of second last option using java script. I wrote something but it is not working .
$('.checkbox4').on('click', function(e) {
var last_chekbox4 = $('.checkbox4:last.prev()');
if (last_chekbox4.is(':checked')) {
$('.checkbox4').prop('checked', false);
$(this).prop('checked', true);
}
});
Upvotes: 0
Views: 795
Reputation: 115282
Use prev()
method
$('.checkbox4:last').prev()
If there is some other element in between then use
$('.checkbox4:last').prevAll('.checkbox4').first()
FYI : Above methods don't work if elements are not siblings.
:not()
and jQuery :last
.
$('.checkbox4:not(:last):last')
Or by the index using eq()
method.
var $col = $('.checkbox4');
// get element by index, where index starts from 0
var $ele = $col.eq($col.length - 2);
Upvotes: 1
Reputation: 12478
You have 7 elements. Use nth-of-type
selector
$(".checkbox4:nth-of-type(6)");
To select the second last option
Upvotes: 0